Ejemplo n.º 1
0
    public override void gaxb_complete()
    {
        ScheduleForStart();

        bool shouldCallSingletonStart = false;

        // If we're in live editor mode, we don't want to load controllers
        if (Application.isPlaying == false)
        {
            base.gaxb_complete();
            return;
        }

        if (gameObject != null)
        {
            gameObject.name = _class;
        }

        if (singleton)
        {
            IPUCode tempClassInstance = (IPUCode)instances [_class];
            if (tempClassInstance != null && !tempClassInstance.Equals(this))
            {
                GameObject.DestroyImmediate(this.gameObject);
                controller = (IPUCode)tempClassInstance;
                shouldCallSingletonStart = true;
            }
            else
            {
                MonoBehaviour.DontDestroyOnLoad(this.gameObject);
                this.gameObject.transform.SetParent(null);
            }
        }


        if (controller == null && _class != null)
        {
            // Attach all of the PlanetUnity objects
            try {
                controller = (IPUCode)gameObject.AddComponent(Type.GetType(_class, true));

                PUGameObject scene = Scope() as PUGameObject;
                if (scene != null)
                {
                    FieldInfo field = controller.GetType().GetField("scene");
                    if (field != null)
                    {
                        field.SetValue(controller, scene);
                    }

                    field = controller.GetType().GetField("puGameObject");
                    if (field != null)
                    {
                        field.SetValue(controller, this);
                    }

                    scene.PerformOnChildren(val =>
                    {
                        PUGameObject oo = val as PUGameObject;
                        if (oo != null && oo.title != null)
                        {
                            field = controller.GetType().GetField(oo.title);
                            if (field != null)
                            {
                                try{
                                    field.SetValue(controller, oo);
                                }catch (Exception e) {
                                    UnityEngine.Debug.Log("Controller error: " + e);
                                }
                            }
                        }
                        return(true);
                    });
                }

                if (singleton)
                {
                    Debug.Log("Saving instance class for: " + _class);
                    instances[_class] = controller;
                }
                else
                {
                    normalInstances[_class] = controller;
                }
            }
            catch (Exception e) {
                UnityEngine.Debug.Log("Controller error: " + e);
            }
        }

        if (controller != null)
        {
            try {
                // Attach all of the named GameObjects
                FieldInfo[] fields = controller.GetType().GetFields();
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType == typeof(GameObject))
                    {
                        GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));

                        foreach (GameObject pObject in pAllObjects)
                        {
                            if (pObject.name.Equals(field.Name))
                            {
                                field.SetValue(controller, pObject);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                UnityEngine.Debug.Log("Controller error: " + e);
            }

            foreach (PUNotification subscribe in Notifications)
            {
                NotificationCenter.addObserver(controller, subscribe.name, Scope(), subscribe.name);
            }
        }

        if (shouldCallSingletonStart)
        {
            GameObject singletonGameObject = GameObject.Find(_class);
            if (singletonGameObject != null)
            {
                singletonGameObject.SendMessage("MarkForCallStart");
            }
        }

        base.gaxb_complete();
    }