Exemplo n.º 1
0
        public override void AddPickerObject(UnityEngine.Object pickerObject)
        {
            base.AddPickerObject(pickerObject);

            if (pickerObject == null)
            {
                return;
            }

            if (!(pickerObject is GameObject))
            {
                EditorUtility.DisplayDialog(GPUInstancerConstants.TEXT_PREFAB_TYPE_WARNING_TITLE, GPUInstancerConstants.TEXT_TREE_PREFAB_TYPE_WARNING, GPUInstancerConstants.TEXT_OK);
                return;
            }

            GameObject prefabObject = (GameObject)pickerObject;

#if UNITY_2018_3_OR_NEWER
            PrefabAssetType prefabType = PrefabUtility.GetPrefabAssetType(pickerObject);

            if (prefabType == PrefabAssetType.Regular || prefabType == PrefabAssetType.Variant || prefabType == PrefabAssetType.Model)
            {
                GameObject newPrefabObject = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(prefabObject);
                if (newPrefabObject != null)
                {
                    while (newPrefabObject.transform.parent != null)
                    {
                        newPrefabObject = newPrefabObject.transform.parent.gameObject;
                    }
                    prefabObject = newPrefabObject;
                }
            }
            else
            {
                EditorUtility.DisplayDialog(GPUInstancerConstants.TEXT_PREFAB_TYPE_WARNING_TITLE, GPUInstancerConstants.TEXT_TREE_PREFAB_TYPE_WARNING, GPUInstancerConstants.TEXT_OK);
                return;
            }
#else
            PrefabType prefabType = PrefabUtility.GetPrefabType(pickerObject);

            if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab)
            {
                bool instanceFound = false;
                if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.ModelPrefabInstance)
                {
#if UNITY_2018_2_OR_NEWER
                    GameObject newPrefabObject = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(prefabObject);
#else
                    GameObject newPrefabObject = (GameObject)PrefabUtility.GetPrefabParent(prefabObject);
#endif
                    if (PrefabUtility.GetPrefabType(newPrefabObject) == PrefabType.Prefab || PrefabUtility.GetPrefabType(newPrefabObject) == PrefabType.ModelPrefab)
                    {
                        while (newPrefabObject.transform.parent != null)
                        {
                            newPrefabObject = newPrefabObject.transform.parent.gameObject;
                        }
                        prefabObject  = newPrefabObject;
                        instanceFound = true;
                    }
                }
                if (!instanceFound)
                {
                    EditorUtility.DisplayDialog(GPUInstancerConstants.TEXT_PREFAB_TYPE_WARNING_TITLE, GPUInstancerConstants.TEXT_TREE_PREFAB_TYPE_WARNING, GPUInstancerConstants.TEXT_OK);
                    return;
                }
            }
#endif

            Undo.RecordObject(this, "Add tree prototype");

            if (terrainSettings != null && terrain != null && terrain.terrainData != null)
            {
                List <TreePrototype> newTreePrototypes = new List <TreePrototype>(terrain.terrainData.treePrototypes);

                TreePrototype terrainTreePrototype = new TreePrototype()
                {
                    prefab     = prefabObject,
                    bendFactor = 0
                };
                newTreePrototypes.Add(terrainTreePrototype);

                terrain.terrainData.treePrototypes = newTreePrototypes.ToArray();
                terrain.terrainData.RefreshPrototypes();
                GPUInstancerUtility.AddTreeInstancePrototypeFromTerrainPrototype(gameObject, prototypeList, terrainTreePrototype, newTreePrototypes.Count - 1, shaderBindings, billboardAtlasBindings, terrainSettings);
            }
        }
        public override void AddPickerObject(UnityEngine.Object pickerObject, GPUInstancerPrototype overridePrototype = null)
        {
            base.AddPickerObject(pickerObject, overridePrototype);

            if (pickerObject == null)
            {
                return;
            }

            if (!(pickerObject is GameObject))
            {
                EditorUtility.DisplayDialog(GPUInstancerConstants.TEXT_PREFAB_TYPE_WARNING_TITLE, GPUInstancerConstants.TEXT_TREE_PREFAB_TYPE_WARNING, GPUInstancerConstants.TEXT_OK);
                return;
            }

            GameObject prefabObject = (GameObject)pickerObject;

#if UNITY_2018_3_OR_NEWER
            PrefabAssetType prefabType = PrefabUtility.GetPrefabAssetType(pickerObject);

            if (prefabType == PrefabAssetType.Regular || prefabType == PrefabAssetType.Variant || prefabType == PrefabAssetType.Model)
            {
                GameObject newPrefabObject = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(prefabObject);
                if (newPrefabObject != null)
                {
                    while (newPrefabObject.transform.parent != null)
                    {
                        newPrefabObject = newPrefabObject.transform.parent.gameObject;
                    }
                    prefabObject = newPrefabObject;
                }
            }
            else
            {
                EditorUtility.DisplayDialog(GPUInstancerConstants.TEXT_PREFAB_TYPE_WARNING_TITLE, GPUInstancerConstants.TEXT_TREE_PREFAB_TYPE_WARNING, GPUInstancerConstants.TEXT_OK);
                return;
            }
#else
            PrefabType prefabType = PrefabUtility.GetPrefabType(pickerObject);

            if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab)
            {
                bool instanceFound = false;
                if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.ModelPrefabInstance)
                {
#if UNITY_2018_2_OR_NEWER
                    GameObject newPrefabObject = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(prefabObject);
#else
                    GameObject newPrefabObject = (GameObject)PrefabUtility.GetPrefabParent(prefabObject);
#endif
                    if (PrefabUtility.GetPrefabType(newPrefabObject) == PrefabType.Prefab || PrefabUtility.GetPrefabType(newPrefabObject) == PrefabType.ModelPrefab)
                    {
                        while (newPrefabObject.transform.parent != null)
                        {
                            newPrefabObject = newPrefabObject.transform.parent.gameObject;
                        }
                        prefabObject  = newPrefabObject;
                        instanceFound = true;
                    }
                }
                if (!instanceFound)
                {
                    EditorUtility.DisplayDialog(GPUInstancerConstants.TEXT_PREFAB_TYPE_WARNING_TITLE, GPUInstancerConstants.TEXT_TREE_PREFAB_TYPE_WARNING, GPUInstancerConstants.TEXT_OK);
                    return;
                }
            }
#endif

            Undo.RecordObject(this, "Add tree prototype");

            if (_treeManager.terrainSettings != null && _treeManager.terrain != null && _treeManager.terrain.terrainData != null)
            {
                if (overridePrototype != null)
                {
                    int prototypeIndex = prototypeList.IndexOf(overridePrototype);
                    if (prototypeIndex >= 0 && prototypeIndex < _treeManager.terrain.terrainData.treePrototypes.Length)
                    {
                        TreePrototype[] treePrototypes = _treeManager.terrain.terrainData.treePrototypes;

                        treePrototypes[prototypeIndex].prefab           = prefabObject;
                        overridePrototype.prefabObject                  = prefabObject;
                        _treeManager.terrain.terrainData.treePrototypes = treePrototypes;
                        _treeManager.terrain.terrainData.RefreshPrototypes();

                        GPUInstancerUtility.DetermineTreePrototypeType(overridePrototype);

                        if (overridePrototype.billboard != null && overridePrototype.useGeneratedBillboard)
                        {
                            GPUInstancerUtility.GeneratePrototypeBillboard(overridePrototype, true);
                        }
                    }
                }
                else
                {
                    List <TreePrototype> newTreePrototypes = new List <TreePrototype>(_treeManager.terrain.terrainData.treePrototypes);

                    TreePrototype terrainTreePrototype = new TreePrototype()
                    {
                        prefab     = prefabObject,
                        bendFactor = 0
                    };
                    newTreePrototypes.Add(terrainTreePrototype);

                    _treeManager.terrain.terrainData.treePrototypes = newTreePrototypes.ToArray();
                    _treeManager.terrain.terrainData.RefreshPrototypes();
                    GPUInstancerUtility.AddTreeInstancePrototypeFromTerrainPrototype(_treeManager.gameObject, prototypeList, terrainTreePrototype,
                                                                                     newTreePrototypes.Count - 1, _treeManager.terrainSettings);
                }
            }
        }