Example #1
0
        public static UnityObject Load(string name)
        {
            // NOTE: If refactoring this please be careful with the loadingScene flag!
            Application.loadingScene = true;
            object o = AssetHelper.Load <object>("Resources", Path.Combine("Resources", name));

            if (o is Scene)
            {
                UnityObject uo = LoadScene(o as Scene);
                Application.loadingScene = false;
                return(uo);
            }
            Application.loadingScene = false;
            if (o == null)
            {
                return(null);
            }
            if (o is UnityObject)
            {
                return(o as UnityObject);
            }
            if (o is Microsoft.Xna.Framework.Graphics.Texture2D)
            {
                Texture2D Tex    = new Texture2D(o as Microsoft.Xna.Framework.Graphics.Texture2D);
                int       iIndex = name.LastIndexOf('/');
                ++iIndex;
                iIndex   = Mathf.Clamp(iIndex, 0, name.Length - 1);
                Tex.name = name.Substring(iIndex);
                return(Tex);
            }
            if (o is SoundEffect)
            {
                return(new AudioClip(o as SoundEffect));
            }
            return(null);
        }
Example #2
0
 /// <summary>
 /// The object obj will be destroyed now or if a time is specified t seconds from now.
 /// If obj is a Component it will remove the component from the GameObject and destroy it.
 /// If obj is a GameObject it will destroy the GameObject, all its components and all transform children of the GameObject.
 /// Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.
 /// </summary>
 /// <param name="obj"></param>
 public static void Destroy(UnityObject obj)
 {
     Destroy(obj, 0.0f);
 }
Example #3
0
 public static void DontDestroyOnLoad(UnityObject target)
 {
     Application.DontDestroyOnLoad(target);
 }
Example #4
0
        internal virtual UnityObject Clone()
        {
            UnityObject obj = MemberwiseClone() as UnityObject;

            return(obj);
        }
Example #5
0
        private void DoFixReferences(object objectToFix, Dictionary <int, UnityObject> idMap)
        {
            List <FieldInfo> memInfo = GetMembersToFix(objectToFix.GetType());

            for (int i = 0; i < memInfo.Count; i++)
            {
                FieldInfo field = memInfo[i];
                if (typeof(UnityObject).IsAssignableFrom(memInfo[i].FieldType))
                {
                    UnityObject val = (field.GetValue(objectToFix) as UnityObject);
                    if (val == null || (val is Asset))
                    {
                        continue;
                    }
                    if (idMap.ContainsKey(val.GetInstanceID()))
                    {
                        if (val != idMap[val.GetInstanceID()])
                        {
                            field.SetValue(objectToFix, idMap[val.GetInstanceID()]);
                        }
                    }
                }
                if (typeof(IList).IsAssignableFrom(field.FieldType))
                {
                    if (field.FieldType.HasElementType && !field.FieldType.GetElementType().IsSubclassOf(typeof(UnityObject)))
                    {
                        continue;
                    }
                    IList list = (memInfo[i].GetValue(objectToFix) as IList);
                    if (list != null)
                    {
                        IList newList;
                        if (list is Array)
                        {
                            newList = (IList)(list as Array).Clone();
                        }
                        else
                        {
                            ConstructorInfo ctor = list.GetType().GetConstructor(new Type[] { typeof(int) });
                            newList = (IList)ctor.Invoke(new object[] { list.Count });
                        }
                        for (int j = 0; j < list.Count; j++)
                        {
                            if (!(newList is Array))
                            {
                                newList.Add(list[j]);
                            }
                            if (list[j] is UnityObject && !(list[j] is Asset))
                            {
                                if (idMap.ContainsKey((list[j] as UnityObject).GetInstanceID()))
                                {
                                    newList[j] = idMap[(list[j] as UnityObject).GetInstanceID()];
                                }
                            }
                        }
                        memInfo[i].SetValue(objectToFix, newList);
                    }
                }

                if (Application.typeCaps.HasCaps(memInfo[i].FieldType, TypeSet.TypeCapabilities.FixReferences))
                {
                    DoFixReferences(memInfo[i].GetValue(objectToFix), idMap);
                }
            }
        }
Example #6
0
 public static void DisplayHierarchy(UnityObject obj)
 {
     gosToDisplay.Add(obj);
 }
Example #7
0
        internal static void CleanUp()
        {
            while (markedForDestruction.Count > 0)
            {
                UnityObject obj = markedForDestruction.Dequeue();
                objects.Remove(obj.GetInstanceID());

                if (obj is GameObject)
                {
                    (obj as GameObject).DoDestroy();
                }

                Component cmp = (obj as Component);
                if (cmp != null)
                {
                    if (cmp is Renderer)
                    {
                        Camera.RemoveRenderer(cmp as Renderer);
                    }

                    if (cmp is Camera)
                    {
                        Camera.RemoveCamera(cmp as Camera);
                    }

                    if (cmp.gameObject != null)
                    {
                        cmp.gameObject.RemoveComponent(cmp);
                    }

                    if (cmp is PressPlay.FFWD.Interfaces.IUpdateable)
                    {
                        PressPlay.FFWD.Interfaces.IUpdateable upd = cmp as PressPlay.FFWD.Interfaces.IUpdateable;
                        if (updateComponents.Contains(upd))
                        {
                            updateComponents.Remove(upd);
                        }
                        if (lateUpdateComponents.Contains(upd))
                        {
                            lateUpdateComponents.Remove(upd);
                        }
                    }

                    if (cmp is IFixedUpdateable)
                    {
                        if (fixedUpdateComponents.Contains(cmp as IFixedUpdateable))
                        {
                            fixedUpdateComponents.Remove(cmp as IFixedUpdateable);
                        }
                    }

                    if (cmp is MonoBehaviour)
                    {
                        if (guiComponents.Contains(cmp as MonoBehaviour))
                        {
                            guiComponents.Remove(cmp as MonoBehaviour);
                        }
                    }

                    for (int j = invokeCalls.Count - 1; j >= 0; j--)
                    {
                        if (invokeCalls[j].behaviour == cmp)
                        {
                            invokeCalls.RemoveAt(j);
                        }
                    }
                }
            }
        }