Esempio n. 1
0
    public void OnGUI()
    {
        GUILayout.Label("Infinite Object Persistence", "BoldLabel");
        GUILayout.Label("1. Generate desired tracks using rules and probabilities");
        GUILayout.Label("2. Hit play in Unity");
        GUILayout.Label("3. Add extra objects (such as tutorial triggers)");
        GUILayout.Label("4. Click \"Persist\"");
        GUILayout.Space(10);
        if (GUILayout.Button("Persist"))
        {
            saveLocation = EditorUtility.SaveFilePanelInProject("Save Location", "InfiniteObjectPersistence", "prefab", "");
            if (saveLocation.Length == 0)
            {
                return;
            }

            GameObject infiniteObjectsGroup = GameObject.Find("Infinite Objects");
            if (infiniteObjectsGroup != null)
            {
                GameObject persistGameObject = new GameObject();

                InfiniteObjectPersistence persistence = persistGameObject.AddComponent <InfiniteObjectPersistence>() as InfiniteObjectPersistence;
                // Persist the Infinite Object Manager Data
                InfiniteObjectGenerator infiniteObjectGenerator = infiniteObjectsGroup.GetComponent <InfiniteObjectGenerator>();
                infiniteObjectGenerator.saveInfiniteObjectPersistence(ref persistence);

                // Persist the Infinite Object History Data
                InfiniteObjectHistory infiniteObjectHistory = infiniteObjectsGroup.GetComponent <InfiniteObjectHistory>();
                infiniteObjectHistory.saveInfiniteObjectPersistence(ref persistence);

                for (int i = infiniteObjectsGroup.transform.childCount - 1; i >= 0; --i)
                {
                    infiniteObjectsGroup.transform.GetChild(i).parent = persistGameObject.transform;
                }

                EditorUtility.SetDirty(persistGameObject);
                PrefabUtility.CreatePrefab(saveLocation, persistGameObject);

                for (int i = persistGameObject.transform.childCount - 1; i >= 0; --i)
                {
                    persistGameObject.transform.GetChild(i).parent = infiniteObjectsGroup.transform;
                }

                DestroyImmediate(persistGameObject);

                Debug.Log("Infinite Object Data Persisted!");
            }
            else
            {
                Debug.Log("Error: Unable to find the Infinite Objects Game Object");
            }
        }
    }