Author Topic: OnGui Equivalent To NGUI, "Displaying image"  (Read 4504 times)

KrakenSpraken

  • Guest
OnGui Equivalent To NGUI, "Displaying image"
« on: November 28, 2013, 01:10:20 AM »
Hello every one is there a equivalent of OnGUI function that used to draw an image to NGUI?
The documentation to the said function is in the link below.
http://docs.unity3d.com/Documentation/Components/gui-Basics.html

Here is the code he used to display the icon
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest : MonoBehaviour {
  5.  
  6.         public Texture2D icon;
  7.  
  8.         void OnGUI () {
  9.                 GUI.Box (new Rect (10,10,100,50), new GUIContent("This is text", icon));
  10.         }
  11.  
  12. }

What I want to do is change the UISprite through button click.
I have seen UISprite and UITexture in NGUI documentation but I do not know what to use.
Any advice or tips will be much appreciated.
Thank you

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #1 on: November 28, 2013, 01:12:41 AM »
If it's a texture that's not within an atlas, use UITexture.

KrakenSpraken

  • Guest
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #2 on: November 28, 2013, 01:23:18 AM »
Thanks a lot ArenMook, Do you have a link to a video that have an in depth discussion or usage of UITexture?
Or even code samples on it.

Thank you ArenMook

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #3 on: November 28, 2013, 01:45:56 AM »
Have you seen the doc page for it? http://www.tasharen.com/forum/index.php?topic=6703.0

KrakenSpraken

  • Guest
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #4 on: November 28, 2013, 01:53:22 AM »
Hi ArenMorok, I try this code to display an image, and i did look at the UITexture at NGUI documentation, I cant seem to find any code there that let me change the texture. Can i have the NGUI syntax code equivalent to the code below?
Thanks a lot and sorry for the hustle.  :-[

  1. public Texture TestTexture;
  2.         // Use this for initialization
  3.         void Start () {
  4.  
  5.                 Object temp = Resources.Load ("davao+rizal+park");
  6.  
  7.                 TestTexture = Resources.Load ("davao+rizal+park") as Texture;
  8.  
  9.                 if(temp == null)
  10.                         Debug.Log("Load Object Fail");
  11.  
  12.  
  13.                 }
  14.  
  15.         void OnGUI () {
  16.                 GUI.Label (new Rect (0,0,200,100), TestTexture);
  17.         }
« Last Edit: November 28, 2013, 03:15:34 AM by Nicki »

zazery

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #5 on: November 28, 2013, 02:23:21 AM »
Read the class documentation too. You just need to set the mainTexture of the UITexture component to the one you loaded from Resources. You might need to call MakePixelPerfect after to adjust it's size or set it's with or height inherited from UIWidget. Here's some untested code.

  1. UITexture textureComponent = GetComponent<UITexture>();
  2. textureComponent.mainTexture = Resource.Load("texturename") as Texture;
  3. textureComponent.MakePixelPerfect();

KrakenSpraken

  • Guest
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #6 on: November 28, 2013, 07:59:58 AM »
Thanks a lot zazery, will do

KrakenSpraken

  • Guest
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #7 on: November 28, 2013, 08:33:12 AM »
Hello i have look at UITexture, what does the get and set mean?
http://tasharen.com/ngui/docs/class_u_i_texture.html

KrakenSpraken

  • Guest
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #8 on: November 28, 2013, 09:26:54 AM »
Hello, This code works but it does not display the texture it just load it, any idea why it wont display?

Quote
UITexture textureComponent = GetComponent<UITexture>();
textureComponent.mainTexture = Resource.Load("texturename") as Texture;
textureComponent.MakePixelPerfect();

KrakenSpraken

  • Guest
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #9 on: November 28, 2013, 09:50:14 AM »
So sorry everyone, i figure out what is wrong, hahaha noob mistakes  :-[

zazery

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: OnGui Equivalent To NGUI, "Displaying image"
« Reply #10 on: November 28, 2013, 02:28:56 PM »
I would recommend reading a book on C# or something to learn the language. Get and set mean mainTexture is a Property (a language feature) and both the get and set accessors are implemented. If you look at UITexture.cs you will see how it's implemented. In fact, whenever I'm not sure how something works I just read the source code. It's really easy to follow and is well written.