Author Topic: NGUI Crashing Unity Editor  (Read 2436 times)

Vesuvian

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
NGUI Crashing Unity Editor
« on: November 21, 2013, 04:48:07 PM »
Unity Pro - 4.2.1f4
NGUI - 3.0.4

Both myself and the other programmer on the project are experiencing Unity crashes that are 100% reproducible in our application. These crashes appear to be caused when instantiating multiple NGUI prefabs.

Our instantiation code:

  1. GameObject prefab = ResourceManager.getResource<GameObject>(path, name);
  2. GameObject instance = NGUITools.AddChild(null, prefab);
  3. return instance.AddComponent<T>();

Our NGUI prefabs are relatively simple, consisting of a Panel with a number of child controls (buttons and labels).

Are there any known issues with using NGUI in this fashion? We are developing a large, complex application with many menus and submenus, which means it is impractical to have all of the menus available in the scene at any given time. If we continue down this route of instantiating NGUI objects on demand are we likely to see more resistance?

I'm going to try and strip down the project to a point where I can post it here.

Thanks,
Ves

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Crashing Unity Editor
« Reply #1 on: November 22, 2013, 03:18:23 AM »
Well, why are you instantiating an NGUI object in the middle of nowhere? It needs to be added as a child of the UI hierarchy, and you are passing 'null' for the parent. What is on the object you're instantiating?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: NGUI Crashing Unity Editor
« Reply #2 on: November 22, 2013, 05:33:03 AM »
There are no known issues with using prefabs and instantiating at run time.

You're not using the AddChild correctly - if you don't want to pass a parent, then don't use AddChild.

  1. AddRootObject(GameObject prefab){
  2.   GameObject go = GameObject.Instantiate(prefab) as GameObject;
  3.   Transform t = go.transform;
  4.   t.localPosition = Vector3.zero;
  5.   t.localRotation = Quaternion.identity;
  6.   t.localScale = Vector3.one;
  7.   go.layer = parent.layer;
  8. return go;
  9. }


Do note that this will just put it in the default layer.


The correct procedure, would be to have a parent defined and use AddChild properly.

Vesuvian

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: NGUI Crashing Unity Editor
« Reply #3 on: November 22, 2013, 10:27:36 AM »
ArenMook, could you please elaborate what you mean by "UI hierarchy"? Does there need to be a specific NGUI component at the top of the hierarchy?

I have started to swap out the bad null parent stuff for what you guys are suggesting, and I have found myself at a point where I can cause the crash on demand again.

I create my "SectionMenuView" object this way:

  1. GameObject prefab = ResourceManager.getResource<GameObject>("Prefabs", "Section");
  2.  
  3. GameObject go = GameObject.Instantiate(prefab) as GameObject;
  4. Transform t = go.transform;
  5. t.localPosition = Vector3.zero;
  6. t.localRotation = Quaternion.identity;
  7. t.localScale = Vector3.one;
  8.  
  9. SectionMenuView sectionMenu = go.AddComponent<SectionMenuView>();
  10.  
  11. sectionMenu.section = section;

Inside the SectionMenuView I proceed to make its child elements:

  1. // Method 1 - Works 100% of the time
  2. GameObject prefab = ResourceManager.getResource<GameObject>("Prefabs", "Cue");
  3. GameObject child = NGUITools.AddChild(this.gameObject, prefab);
  4. CueView cueView = child.AddComponent<CueView>();
  5.  
  6. // Method 2 - Crashes 100% of the time
  7. CueView cue = CueView.newInstance<CueView>(this.gameObject, "Prefabs", "Cue");

I have modified the newInstance method:

  1. public static new T newInstance<T>(GameObject parent, string path, string name)
  2.         where T : MonoBehaviour
  3. {
  4.         GameObject prefab = ResourceManager.getResource<GameObject>(path, name);
  5.         GameObject instance = NGUITools.AddChild(parent, prefab);
  6.        
  7.         return instance.AddComponent<T>();
  8. }

As far as I can tell the newInstance method does exactly the same thing as the working lines in the SectionMenuView.

Can you guys spot anything I'm doing wrong in the newInstance method?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Crashing Unity Editor
« Reply #4 on: November 22, 2013, 03:45:30 PM »
There needs to be a specific hierarchy, yes. You must always have at least one game object above your widgets, or a panel can't be created. For example this is valid hierarchy:

GameObject (UIPanel)
- UIWidget

...but this is not:

UIWidget
- UIWidget