Author Topic: *Solved* Instance of NGUI element  (Read 25035 times)

Ingot

  • Guest
*Solved* Instance of NGUI element
« on: June 20, 2012, 02:49:28 PM »
I am trying to use an NGUI element as an object that instantiates and moves to the center of the screen before destroying.
I was able to make the instances by prefabbing the NUGI element, which contains a UISprite script, and using TweenPosition
to move the object as I would like it. The obejct appears and moves as it should, but the sprite is not visible and I wonder
if it is because I prefabbed and instanced it as a GameObject. What can I do to make this work?

I instantiate the prefab and make it a child of the panel and reset its transforms like so:

  1. sparkList.Add(Instantiate(sparkPrefab1) as GameObject);
  2. sparkList[sparksIndex].transform.parent = parent.transform;
  3.                        
  4. sparkList[sparksIndex].transform.rotation = parent.rotation;
  5. sparkList[sparksIndex].transform.localPosition = Vector3.zero;
  6. sparkList[sparksIndex].transform.localScale = Vector3.one;
  7.  
« Last Edit: June 21, 2012, 07:40:47 PM by Ingot »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instance of NGUI element
« Reply #1 on: June 20, 2012, 09:26:54 PM »
First of all, use NGUITools.AddChild to instantiate prefabs. Second, make sure that the widget's layer is correct. Here you instantiate some game object and parent it to some other game object. If there is no UI panel in the hierarchy, one will be added automatically at the root-most object, assuming the layer of that object to be used for the rest of the UI. Your entire hierarchy must be on the UI layer visible by your UI camera.

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #2 on: June 20, 2012, 10:16:11 PM »
Ok,
I replaced the instantiate method with this:
sparkList.Add(NGUITools.AddChild(sparkPrefab1));

But, it throws this error:
"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption".

Which points to this line in the NGUITools.cs file:
t.parent = parent.transform; (Line 367)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instance of NGUI element
« Reply #3 on: June 20, 2012, 10:16:58 PM »
AddChild accepts 2 parameters -- the parent, and the prefab.

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #4 on: June 20, 2012, 10:17:28 PM »
Ok, the object is spawning correctly and moving to the middle as before, but the sprite is still not visible.

After using this line:

sparkList.Add(NGUITools.AddChild(GameObject.Find("Panel"), sparkPrefab1));
(I will cache that later)

I believe the layers and the camera culling is set up properly, but I will double check all that.
« Last Edit: June 20, 2012, 10:30:21 PM by Ingot »

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #5 on: June 20, 2012, 10:26:36 PM »
Yes, that is all set up correctly.
Everything works except that I can not see the object's image as it moves across the screen.
The layer for the backdrop, which is the only thing that would conflict with it, is set to 1 and
the object itself is set to 6. The UIRoot camera is set to display the object, so I would think
that it should work fine. Also, the object spawns under the Panel as it should.
« Last Edit: June 20, 2012, 10:29:25 PM by Ingot »

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #6 on: June 21, 2012, 01:53:31 PM »
I still need help understanding this please.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instance of NGUI element
« Reply #7 on: June 21, 2012, 02:32:30 PM »
"The layer for the backdrop"?

Do you mean depth? Keep in mind depth only works within the same panel, and only for widgets using the same material (atlas).

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #8 on: June 21, 2012, 02:35:17 PM »
Ya, I meant the depth.
All of the elements are under the UI Root (2D) / Camera / Anchor / Panel
and the object in question instantiates under Panel in the hierarchy.
« Last Edit: June 21, 2012, 02:36:49 PM by Ingot »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instance of NGUI element
« Reply #9 on: June 21, 2012, 02:40:19 PM »
If you see it in the scene view but don't see it in the game view, then the problem is not with NGUI. You say you've checked everything, so I don't know what else to tell you.

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #10 on: June 21, 2012, 02:43:36 PM »
Ok, I want to include a screen shot to see if you can tell what the matter is:

*Deleted*

See the hierarchy, and the empty object that is just to the left along the green bar.
The two sparks that are near the center are working properly and moving to the
center as they are expected to according to the Tween script. Those two were
manually placed in the panel just outside the viewing area. However, the
instantiated one (Spark 0) does not show it's sprite like the other two.
« Last Edit: June 21, 2012, 04:59:41 PM by Ingot »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instance of NGUI element
« Reply #11 on: June 21, 2012, 02:45:30 PM »
Its scale is (1, 1, 1), meaning it's one pixel in size.

When instantiating anything, make sure you instantiate its parent -- never instantiate widgets themselves.

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #12 on: June 21, 2012, 02:49:43 PM »
I don't quite understand what you are telling me I should do there when you say instantiate its parent and not the widget.
Are you saying that I should re-instantiate the Panel? I don't get it. After manually scaling the Spark up to 52 x 52 like the
others, it is still just an empty square without an image.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instance of NGUI element
« Reply #13 on: June 21, 2012, 02:51:27 PM »
I mean create a prefab that has a game object, and this game object would have widget children. The game object would have a scale of (1, 1, 1). That's also the game object that you would be tweening.

Ingot

  • Guest
Re: Instance of NGUI element
« Reply #14 on: June 21, 2012, 02:53:15 PM »
Ok, I think I understand.
I will try that thank you.

That got it working, thank you very much!
« Last Edit: June 21, 2012, 03:47:19 PM by Ingot »