Ejemplo n.º 1
0
 /// <summary>
 /// Generates a cache of instances for the prefab (creating a new pool if needed).
 /// </summary>
 /// <param name="original"></param>
 /// <param name="count"></param>
 public static void PreCacheInstances(Object original, int count)
 {
     count = Mathf.Max(0, count);
     if (original is Component || original is GameObject)
     {
         GameObject    go      = GetObjectAsGameObject(original);
         PoolBehaviour poolBhv = FindOrCreatePool(go);
         if (poolBhv)
         {
             poolBhv.PreCacheInstances(count);
         }
     }
 }
Ejemplo n.º 2
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUI.changed)
            {
                //TODO: initialize only if prefab changes
                m_target.Initialize();
            }

            int preCachedCount = EditorGUILayout.DelayedIntField("Pre-Cached Instances", m_target.transform.childCount);

            if (preCachedCount != m_target.transform.childCount)
            {
                m_target.PreCacheInstances(preCachedCount);
            }

            if (GUILayout.Button("Add Scene Instances to Pool"))
            {
                List <GameObject> gameObjects = GetAllObjectsInScene();
                foreach (GameObject go in gameObjects)
                {
                    //Debug.Log(go.name + " --> " + PrefabUtility.GetPrefabObject(go) + ", " + PrefabUtility.GetPrefabParent(go) + ", " + PrefabUtility.GetPrefabType(go));
#if UNITY_2018_3_OR_NEWER
                    if (PrefabUtility.GetCorrespondingObjectFromSource(go) == m_target.Prefab)
#else
                    if (PrefabUtility.GetPrefabParent(go) == m_target.Prefab)
#endif
                    {
                        if (m_target.TryAddGameObjectToPool(go))
                        {
                            Debug.Log("Found " + go.name);
                        }
                    }
                }
            }
        }