1
\$\begingroup\$

How can I change the texture at runtime. I have done changing the label(text) at runtime. The following code is used

using UnityEngine;
using System.Collections;

public class switchtime : MonoBehaviour {

    // Use this for initialization
    private bool showLabel = false;
    private bool showLabe2 = false;
    private bool showLabe3 = false;
    private bool showLabe4 = false;

    public void Start() {
        Invoke("ToggleLabel", 1);
        Invoke("ToggleLabel2", 3);
        Invoke("ToggleLabel3",6 );
        Invoke("ToggleLabel4", 9);
    }

    public void ToggleLabel() {
        showLabel = !showLabel;

    }
    public void ToggleLabel2() {

        showLabe2 = !showLabe2;
    }
    public void ToggleLabel3() {

        showLabe3 = !showLabe3;
    }
    public void ToggleLabel4() {

        showLabe4 = !showLabe4;
    }

    public void OnGUI() {
        if (showLabel) {
            GUI.Label(new Rect(300, 200, 100, 20), "Copying window file..");

        }
        if (showLabe2) {

            GUI.Label(new Rect(40, 40, 100, 20), "Epanding windows file..");
        }
        if (showLabe3) {

            GUI.Label(new Rect(80, 80, 100, 20), "Installing Feature..");
        }
        if (showLabe4) {

            GUI.Label(new Rect(100, 100, 100, 20), "Installing Updates");
        }
    }
}

Now I need to change the GUITexture at runtime. How can do this? Can anybody help me in coding?

Here is some changes I made. I worked on changing the gameObject when mouse is placed on it. This is the code I applied and its working for me

using UnityEngine;
using System.Collections;

public class change : MonoBehaviour {

    // Use this for initialization
    public GameObject newSprite;

    private Vector3 currentSpritePosition;
    void update()
    {    
    }

    void Start()
    {    
        newSprite.renderer.enabled = false;
        currentSpritePosition = transform.position;

        //then make it invisible
        renderer.enabled = false;

        //give the new sprite the position of the latter
        newSprite.transform.position = currentSpritePosition;

        //then make it visible
        newSprite.renderer.enabled = true;
    }

    void OnMouseExit(){
        //just the reverse process
        renderer.enabled = true;
        newSprite.renderer.enabled = false;
    }
}

Now the problem is that, in same code I need to set a time so that the text get loaded without the mouse click. I mean a particular time to load each text one after the another.

Here is my screen shot enter image description here

Here I have placed the image one below the another,when the mouse is hovered on it it will change the text from bold to normal text.Changes process stated in (image 2,3).That code I posted is working for me.

\$\endgroup\$
4
  • \$\begingroup\$ The last part is not clear, are you wanting to reveal text on a mouse click? Are you trying to animate text after a mouse click? Are there any examples of the effect you are trying to achieve? \$\endgroup\$ Jul 21, 2014 at 12:53
  • \$\begingroup\$ the above example i have done and got the object changed when the mouse is pointed on it.Above I have placed the scrren shot \$\endgroup\$ Jul 21, 2014 at 12:55
  • \$\begingroup\$ From the image,mage(1) first text is normal font and all other are in bold font.When the mouse is moved on the text it will change from bold to normal text. \$\endgroup\$ Jul 21, 2014 at 13:01
  • \$\begingroup\$ I'm not entirely sure why you are doing that sort of work in Unity3D but alright :) Instead of doing an edit please create an answer with that content and accept it. \$\endgroup\$ Jul 21, 2014 at 13:03

1 Answer 1

1
\$\begingroup\$

You can load resources from a folder called "Resources" using this:

Texture texture = Resources.Load(textureName);
guiTexture.texture = texture;

Nice and simple. If you want to find a particular texture using a named reference you can use:

Texture texture = Resources.Load(textureName);
GameObject.Find("NameRef").guiTexture.texture = texture;

You can even shorthand it to:

GameObject.Find("NameRef").guiTexture.texture
    = Resources.Load(textureName, typeof(Texture));

EDIT:

You can also apply this to an attached texture:

using UnityEngine;
using System.Collections;

public class switchtime : MonoBehaviour {

    public GUITexture myGuiTexture;

    public void OnGUI()
    {
        // Blah blah do other stuff

        ApplyMyTexture();
    }

    public void ApplyMyTexture()
    {
        myGuiTexture.texture
            = Resources.Load(textureName, typeof(Texture));
    }
}

You can change the function names to suit your usage.

\$\endgroup\$
3
  • \$\begingroup\$ Hope my question was not clear \$\endgroup\$ Jul 21, 2014 at 12:43
  • \$\begingroup\$ I'm not sure what you mean? Your question was clear. You want to change a texture on your GUI at runtime, this is how you do it. This also assumes that you have added a Texture to your scene. If you add a GUITexture to the scene and give it a name you can reference it using these functions. You can also add it as a property on your game object that this script is attached to so that you can reference it directly. \$\endgroup\$ Jul 21, 2014 at 12:44
  • 1
    \$\begingroup\$ above I have edited my question and placed my screenshot.Hope it will be more clear \$\endgroup\$ Jul 21, 2014 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .