Пример #1
0
        public static void GetSceneDependencies(string scenePath, List <Object> dependencies)
        {
            var dependencyPaths = AssetDatabase.GetDependencies(scenePath);

            // Convert the dependency paths to assets
            // Remove scene from dependencies
            foreach (var dependencyPath in dependencyPaths)
            {
                if (dependencyPath.Equals(scenePath))
                {
                    continue;
                }

                var dependencyType = AssetDatabase.GetMainAssetTypeAtPath(dependencyPath);
                if (dependencyType == null)
                {
                    continue;
                }
                var typeInfo = SceneTemplateProjectSettings.Get().GetDependencyInfo(dependencyType);
                if (typeInfo.ignore)
                {
                    continue;
                }
                var obj = AssetDatabase.LoadAssetAtPath(dependencyPath, dependencyType);
                dependencies.Add(obj);
            }
        }
Пример #2
0
        private static List <SceneTemplateInfo> GetSceneTemplateInfos()
        {
            var sceneTemplateList = new List <SceneTemplateInfo>();

            // Add the special Empty and Basic template
            s_EmptySceneTemplateInfo.isPinned = SceneTemplateProjectSettings.Get().GetPinState(s_EmptySceneTemplateInfo.name);
            s_BasicSceneTemplateInfo.isPinned = SceneTemplateProjectSettings.Get().GetPinState(s_BasicSceneTemplateInfo.name);

            // Check for real templateAssets:
            var sceneTemplateAssetInfos = SceneTemplateUtils.GetSceneTemplatePaths().Select(templateAssetPath =>
            {
                var sceneTemplateAsset = AssetDatabase.LoadAssetAtPath <SceneTemplateAsset>(templateAssetPath);
                return(Tuple.Create(templateAssetPath, sceneTemplateAsset));
            })
                                          .Where(templateData => {
                if (templateData.Item2 == null)
                {
                    return(false);
                }
                if (!templateData.Item2.IsValid)
                {
                    return(false);
                }
                var pipeline = templateData.Item2.CreatePipeline();
                if (pipeline == null)
                {
                    return(true);
                }
                return(pipeline.IsValidTemplateForInstantiation(templateData.Item2));
            }).
                                          Select(templateData =>
            {
                var assetName = Path.GetFileNameWithoutExtension(templateData.Item1);

                var isReadOnly = false;
                if (templateData.Item1.StartsWith("Packages/") && AssetDatabase.GetAssetFolderInfo(templateData.Item1, out var isRootFolder, out var isImmutable))
                {
                    isReadOnly = isImmutable;
                }

                return(new SceneTemplateInfo {
                    name = string.IsNullOrEmpty(templateData.Item2.templateName) ? assetName : templateData.Item2.templateName,
                    isPinned = templateData.Item2.addToDefaults,
                    isReadonly = isReadOnly,
                    assetPath = templateData.Item1,
                    description = templateData.Item2.description,
                    thumbnail = templateData.Item2.preview,
                    sceneTemplate = templateData.Item2,
                    onCreateCallback = loadAdditively => CreateSceneFromTemplate(templateData.Item1, loadAdditively)
                });
            }).ToList();

            sceneTemplateAssetInfos.Sort();
            sceneTemplateList.AddRange(sceneTemplateAssetInfos);

            sceneTemplateList.Add(s_EmptySceneTemplateInfo);
            sceneTemplateList.Add(s_BasicSceneTemplateInfo);

            return(sceneTemplateList);
        }
Пример #3
0
        private bool AreAllDependenciesCloned()
        {
            var sceneTemplateAsset = (SceneTemplateAsset)serializedObject.targetObject;

            return(sceneTemplateAsset.dependencies
                   .Where(dep => SceneTemplateProjectSettings.Get().GetDependencyInfo(dep.dependency).supportsModification)
                   .All(dep => dep.instantiationMode == TemplateInstantiationMode.Clone));
        }
Пример #4
0
        public static void Save(string path = null, SceneTemplateProjectSettings settings = null)
        {
            path     = path ?? k_Path;
            settings = settings ?? Get();
            var json = JsonUtility.ToJson(settings, true);

            System.IO.File.WriteAllText(path, json);
        }
Пример #5
0
        internal static void Reset()
        {
            if (System.IO.File.Exists(k_Path))
            {
                System.IO.File.Delete(k_Path);
            }

            m_Instance = null;
        }
        internal static void Reset()
        {
            if (System.IO.File.Exists(SceneTemplateProjectSettings.k_Path))
            {
                System.IO.File.Delete(SceneTemplateProjectSettings.k_Path);
            }

            m_Instance = null;
        }
 bool AreAllFilteredDependenciesCloned()
 {
     return(m_FilteredItems.Where(sp =>
     {
         var depProperty = sp.FindPropertyRelative(SceneTemplateUtils.DependencyPropertyName);
         return SceneTemplateProjectSettings.Get().GetDependencyInfo(depProperty.objectReferenceValue).supportsModification;
     }).All(sp =>
     {
         var instantiationModeProperty = sp.FindPropertyRelative(SceneTemplateUtils.InstantiationModePropertyName);
         return IsCloning(instantiationModeProperty);
     }));
 }
Пример #8
0
        /// <summary>
        /// Refresh all the dependencies of the template.
        /// </summary>
        public void UpdateDependencies()
        {
            if (!IsValid)
            {
                dependencies = new DependencyInfo[0];
                return;
            }

            var scenePath = AssetDatabase.GetAssetPath(templateScene.GetInstanceID());

            if (string.IsNullOrEmpty(scenePath))
            {
                dependencies = new DependencyInfo[0];
                return;
            }

            var sceneName   = Path.GetFileNameWithoutExtension(scenePath);
            var sceneFolder = Path.GetDirectoryName(scenePath).Replace("\\", "/");
            var sceneCloneableDependenciesFolder = Path.Combine(sceneFolder, sceneName).Replace("\\", "/");

            var depList = new List <Object>();

            ReferenceUtils.GetSceneDependencies(scenePath, depList);

            dependencies = depList.Select(d =>
            {
                var oldDependencyInfo = dependencies.FirstOrDefault(di => di.dependency.GetInstanceID() == d.GetInstanceID());
                if (oldDependencyInfo != null)
                {
                    return(oldDependencyInfo);
                }

                var depTypeInfo       = SceneTemplateProjectSettings.Get().GetDependencyInfo(d);
                var dependencyPath    = AssetDatabase.GetAssetPath(d);
                var instantiationMode = depTypeInfo.defaultInstantiationMode;
                if (depTypeInfo.supportsModification && !string.IsNullOrEmpty(dependencyPath))
                {
                    var assetFolder = Path.GetDirectoryName(dependencyPath).Replace("\\", "/");
                    if (assetFolder == sceneCloneableDependenciesFolder)
                    {
                        instantiationMode = TemplateInstantiationMode.Clone;
                    }
                }

                return(new DependencyInfo()
                {
                    dependency = d,
                    instantiationMode = instantiationMode
                });
            }).ToArray();
        }
        private static void PopulateDependencyProperties(SerializedProperty rootProperty, List <SerializedProperty> model)
        {
            var numDependency = rootProperty.arraySize;

            for (var i = 0; i < numDependency; ++i)
            {
                var depInfoProperty = rootProperty.GetArrayElementAtIndex(i);
                var depProperty     = depInfoProperty.FindPropertyRelative(SceneTemplateUtils.DependencyPropertyName);
                if (!SceneTemplateProjectSettings.Get().GetDependencyInfo(depProperty.objectReferenceValue).ignore)
                {
                    model.Add(depInfoProperty);
                }
            }
        }
Пример #10
0
 internal static void FileNewScene()
 {
     if (SceneTemplateProjectSettings.Get().newSceneOverride == SceneTemplateProjectSettings.NewSceneOverride.BuiltinScene)
     {
         EditorApplication.FileMenuNewScene();
     }
     else if (!EditorApplication.isPlaying)
     {
         SceneTemplateDialog.ShowWindow();
     }
     else
     {
         Debug.LogWarning("Cannot open the New Scene dialog while playing.");
     }
 }
Пример #11
0
        public static SceneTemplateProjectSettings Get()
        {
            if (m_Instance == null)
            {
                InitDefaultDependencyTypeInfos();

                m_Instance = Load(k_Path);
                if (m_Instance == null)
                {
                    m_Instance = new SceneTemplateProjectSettings();
                }

                m_Instance.SetupDependencyTypeInfos();
            }

            return(m_Instance);
        }
Пример #12
0
        private void OnPinnedChanged(GridView.Item item, bool isPinned)
        {
            var info = (SceneTemplateInfo)item.userData;

            if (info.IsInMemoryScene || info.isReadonly)
            {
                SceneTemplateProjectSettings.Get().SetPinState(info.name, isPinned);
            }
            else
            {
                var infoObj = new SerializedObject(info.sceneTemplate);
                var prop    = infoObj.FindProperty("addToDefaults");
                prop.boolValue = isPinned;
                infoObj.ApplyModifiedProperties();
                OnSceneTemplateAssetModified(info.sceneTemplate);
            }
        }
        private static void LoadDependencies()
        {
            if (!Selection.activeObject || Selection.activeObject == null)
            {
                return;
            }

            var currentSelectionPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (string.IsNullOrEmpty(currentSelectionPath))
            {
                return;
            }

            var dependencies  = AssetDatabase.GetDependencies(currentSelectionPath);
            var stringBuilder = new StringBuilder();

            foreach (var depPath in dependencies)
            {
                var obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(depPath);
                if (!obj || obj == null)
                {
                    continue;
                }

                if (currentSelectionPath == depPath)
                {
                    continue;
                }

                var typeInfo = SceneTemplateProjectSettings.Get().GetDependencyInfo(obj);
                if (typeInfo.ignore)
                {
                    continue;
                }

                stringBuilder.AppendLine($"name: {obj.name} - path {depPath}");
            }
            Debug.Log(stringBuilder.ToString());
        }
        void BindItem(VisualElement el, int modelIndex)
        {
            var property    = m_FilteredItems[modelIndex];
            var rowItem     = (DependencyRowItem)el;
            var depProperty = property.FindPropertyRelative(SceneTemplateUtils.DependencyPropertyName);
            var refValue    = depProperty.objectReferenceValue;
            var type        = refValue.GetType();
            var content     = EditorGUIUtility.ObjectContent(refValue, type);

            rowItem.icon.image        = content.image;
            rowItem.label.text        = content.text;
            rowItem.label.tooltip     = AssetDatabase.GetAssetPath(refValue);
            rowItem.typeLabel.text    = type.Name;
            rowItem.typeLabel.tooltip = type.FullName;
            rowItem.userData          = property;

            var instantiationModeProperty = property.FindPropertyRelative(SceneTemplateUtils.InstantiationModePropertyName);

            rowItem.cloneToggle.value = IsCloning(instantiationModeProperty);
            rowItem.cloneToggle.SetEnabled(SceneTemplateProjectSettings.Get().GetDependencyInfo(depProperty.objectReferenceValue).supportsModification);
            rowItem.cloneToggle.RegisterValueChangedCallback(evt =>
            {
                if (evt.newValue == IsCloning(instantiationModeProperty))
                {
                    return;
                }
                var newInstantiationType = (evt.newValue ? TemplateInstantiationMode.Clone : TemplateInstantiationMode.Reference);
                instantiationModeProperty.enumValueIndex = (int)newInstantiationType;

                // Sync Selection if the dependency is part of it:
                if (listView.selectedIndices.Contains(modelIndex))
                {
                    SyncListSelectionToValue(newInstantiationType);
                }
                m_SerializedObject.ApplyModifiedProperties();

                UpdateGlobalCloneToggle();
            });
        }
Пример #15
0
        void BindDependencyElement(VisualElement el, int modelIndex)
        {
            var property    = m_DependenciesProperty[modelIndex];
            var icon        = (Image)el.ElementAt(0);
            var label       = (Label)el.ElementAt(1);
            var depProperty = property.FindPropertyRelative(k_DependencyPropertyName);
            var content     = EditorGUIUtility.ObjectContent(depProperty.objectReferenceValue, depProperty.objectReferenceValue.GetType());

            icon.image = content.image;
            label.text = content.text;
#if SCENE_TEMPLATE_DEBUG
            label.text += $"({depProperty.objectReferenceValue.GetType().ToString()})";
#endif
            el.userData = property;

            var instantiationModeProperty = property.FindPropertyRelative(k_InstantiationModePropertyName);
            var cloneToggle = (Toggle)el.ElementAt(2);
            cloneToggle.value = IsCloning(instantiationModeProperty);
            cloneToggle.SetEnabled(SceneTemplateProjectSettings.Get().GetDependencyInfo(depProperty.objectReferenceValue).supportsModification);
            cloneToggle.RegisterValueChangedCallback <bool>(evt =>
            {
                if (evt.newValue == IsCloning(instantiationModeProperty))
                {
                    return;
                }
                var newInstantiationType = (evt.newValue ? TemplateInstantiationMode.Clone : TemplateInstantiationMode.Reference);
                instantiationModeProperty.enumValueIndex = (int)newInstantiationType;

                // Sync Selection if the dependency is part of it:
                if (m_ZebraList.listView.selectedIndices.Contains(modelIndex))
                {
                    SyncListSelectionToValue(newInstantiationType);
                }
                serializedObject.ApplyModifiedProperties();

                m_CloneHeaderToggle.SetValueWithoutNotify(AreAllDependenciesCloned());
            });
        }
        internal static List <SceneTemplateInfo> GetSceneTemplateInfos()
        {
            var sceneTemplateList = new List <SceneTemplateInfo>();

            // Add the special Empty and Basic template

            foreach (var builtinTemplateInfo in builtinTemplateInfos)
            {
                builtinTemplateInfo.isPinned = SceneTemplateProjectSettings.Get().GetPinState(builtinTemplateInfo.name);
            }

            // Check for real templateAssets:
            var sceneTemplateAssetInfos = GetSceneTemplates().Select(sceneTemplateAsset =>
            {
                var templateAssetPath = AssetDatabase.GetAssetPath(sceneTemplateAsset.GetInstanceID());
                return(Tuple.Create(templateAssetPath, sceneTemplateAsset));
            })
                                          .Where(templateData =>
            {
                if (templateData.Item2 == null)
                {
                    return(false);
                }
                if (!templateData.Item2.isValid)
                {
                    return(false);
                }
                var pipeline = templateData.Item2.CreatePipeline();
                if (pipeline == null)
                {
                    return(true);
                }
                return(pipeline.IsValidTemplateForInstantiation(templateData.Item2));
            }).
                                          Select(templateData =>
            {
                var assetName = Path.GetFileNameWithoutExtension(templateData.Item1);

                var isReadOnly = false;
                if (templateData.Item1.StartsWith("Packages/") && AssetDatabase.GetAssetFolderInfo(templateData.Item1, out var isRootFolder, out var isImmutable))
                {
                    isReadOnly = isImmutable;
                }

                return(new SceneTemplateInfo
                {
                    name = string.IsNullOrEmpty(templateData.Item2.templateName) ? assetName : templateData.Item2.templateName,
                    isPinned = templateData.Item2.addToDefaults,
                    isReadonly = isReadOnly,
                    assetPath = templateData.Item1,
                    description = templateData.Item2.description,
                    thumbnail = templateData.Item2.preview,
                    badge = templateData.Item2.badge,
                    sceneTemplate = templateData.Item2,
                    onCreateCallback = loadAdditively => CreateSceneFromTemplate(templateData.Item1, loadAdditively)
                });
            }).ToList();

            sceneTemplateAssetInfos.Sort();
            sceneTemplateList.AddRange(sceneTemplateAssetInfos);

            if (EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D)
            {
                sceneTemplateList.AddRange(builtin2DTemplateInfos);
            }
            else
            {
                sceneTemplateList.AddRange(builtin3DTemplateInfos);
            }

            return(sceneTemplateList);
        }
        public static void Save(string path, SceneTemplateProjectSettings settings)
        {
            var json = JsonUtility.ToJson(settings, true);

            System.IO.File.WriteAllText(path, json);
        }