Author Topic: how can i make sprite dynamically?  (Read 3108 times)

Uther

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
how can i make sprite dynamically?
« on: March 04, 2014, 03:28:19 AM »
Hello.


When I try to click on button, i would like to create sprite dynamically.
please give me an example or explanation or link for help.


Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how can i make sprite dynamically?
« Reply #1 on: March 04, 2014, 03:34:09 AM »
  1. UISprite sprite = NGUITools.AddWidget<UISprite>(gameObject);

Uther

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: how can i make sprite dynamically?
« Reply #2 on: March 04, 2014, 04:07:17 AM »
Thank you for your quick answer.

but with your code, I tried and saw something created in Hierarchy but i found some error message in console.

- My code:
      GameObject go = Resources.Load ("Prefabs/pf_main_pencil") as GameObject;
      UISprite sp1 = NGUITools.AddWidget<UISprite>(go);

- error message in console:

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
UnityEngine.Transform:set_parent(Transform)
NGUITools:AddChild(GameObject, Boolean) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:371)
NGUITools:AddChild(GameObject) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:356)
NGUITools:AddChild(GameObject) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:752)

PS. I am using the latest NGUI version in Unity 4.2 , which I purchased two weeks ago.
NGUITools:AddWidget(GameObject) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:777)


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how can i make sprite dynamically?
« Reply #3 on: March 04, 2014, 08:36:35 AM »
You must be new to Unity.

Resources.Load gives you a prefab. You can't modify this prefab. You have to instantiate it.

NGUITools.AddWidget adds a child widget to the target game object. You are attempting to add a child object to a prefab, and that's not something that's allowed.

You need to instantiate the prefab first before making any changes to it. Please consult the Unity's documentation regarding Resources.Load.

Uther

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: how can i make sprite dynamically?
« Reply #4 on: March 04, 2014, 08:34:16 PM »
Thank you.
i got your point well.