Esempio n. 1
0
//----------------------------------------------------------------------------------------------------------------------

        //Support Toggle, FloatField, etc
        private F AddField <F, V>(VisualTreeAsset template, VisualElement parent, string text,
                                  Action <MeshSyncPlayerConfig, V> onValueChanged) where F : VisualElement, INotifyValueChanged <V>, new()
        {
            TemplateContainer templateInstance = template.CloneTree();
            VisualElement     fieldContainer   = templateInstance.Query <VisualElement>("FieldContainer").First();
//		F field = templateInstance.Query<F>().First();
            Label label = templateInstance.Query <Label>().First();

            label.text = text;

            F field = new F();

            field.AddToClassList("general-settings-field");
            field.RegisterValueChangedCallback((ChangeEvent <V> changeEvent) => {
                MeshSyncPlayerConfig config = field.userData as MeshSyncPlayerConfig;
                if (null == config)
                {
                    Debug.LogError("[MeshSync] Field doesn't have the correct user data");
                    return;
                }

                onValueChanged(config, changeEvent.newValue);
                MeshSyncProjectSettings.GetOrCreateSettings().SaveSettings();
            });

            fieldContainer.Add(field);
            parent.Add(templateInstance);
            m_playerConfigUIElements.Add(field);
            return(field);
        }
Esempio n. 2
0
        static void CreateSceneCachePlayerMenu(MenuCommand menuCommand)
        {
            string sceneCacheFilePath = EditorUtility.OpenFilePanelWithFilters("Select Cache File", "",
                                                                               new string[] { "All supported files", "sc", "All files", "*" });

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

            //Prefab and assets path
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();

            string scOutputPath   = projectSettings.GetSceneCacheOutputPath();
            string prefabFileName = Path.GetFileNameWithoutExtension(sceneCacheFilePath);
            string prefabPath     = $"{scOutputPath}/{prefabFileName}.prefab";
            string assetsFolder   = Path.Combine(scOutputPath, prefabFileName);

            bool created = SceneCachePlayerEditorUtility.CreateSceneCachePlayerAndPrefab(sceneCacheFilePath, prefabPath,
                                                                                         assetsFolder, out SceneCachePlayer player, out GameObject prefab);


            if (!created)
            {
                EditorUtility.DisplayDialog("MeshSync"
                                            , "Failed to open " + sceneCacheFilePath
                                            + ". Possible reasons: file format version does not match, or the file is not scene cache."
                                            , "Ok"
                                            );
            }
        }
Esempio n. 3
0
//----------------------------------------------------------------------------------------------------------------------
        private PopupField <T> AddPopupField <T>(VisualTreeAsset template, VisualElement parent, string text,
                                                 List <T> options, Action <MeshSyncPlayerConfig, int> onValueChanged)
        {
            TemplateContainer templateInstance = template.CloneTree();
            VisualElement     fieldContainer   = templateInstance.Query <VisualElement>("FieldContainer").First();
            PopupField <T>    popupField       = new PopupField <T>(options, options[0]);

            popupField.AddToClassList("general-settings-field");

            Label label = templateInstance.Query <Label>().First();

            label.text = text;
            popupField.RegisterValueChangedCallback((ChangeEvent <T> changeEvent) => {
                MeshSyncPlayerConfig config = popupField.userData as MeshSyncPlayerConfig;
                if (null == config)
                {
                    Debug.LogError("[MeshSync] Toggle doesn't have the correct user data");
                    return;
                }

                onValueChanged(config, popupField.index);
                MeshSyncProjectSettings.GetOrCreateSettings().SaveSettings();
            });

            fieldContainer.Add(popupField);
            parent.Add(templateInstance);
            m_playerConfigUIElements.Add(popupField);
            return(popupField);
        }
//----------------------------------------------------------------------------------------------------------------------
        void OnOutputPathSelectButtonClicked()
        {
            string path = EditorUtility.OpenFolderPanel("Select Scene Cache Output Path",
                                                        m_generatedSCResPathTextField.value,
                                                        "");

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

            if (!path.StartsWith(Application.dataPath))
            {
                EditorUtility.DisplayDialog("MeshSync",
                                            $"Invalid path: {path}. " +
                                            "Path has to be under the Assets folder.",
                                            "Ok");
                return;
            }

            MeshSyncProjectSettings settings = MeshSyncProjectSettings.GetOrCreateSettings();

            path = AssetEditorUtility.NormalizePath(path);
            settings.SetSceneCacheOutputPath(path);
            settings.Save();

            RefreshSettings();
        }
Esempio n. 5
0
        public void CreateRuntimeSettings()
        {
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();

            Assert.NotNull(projectSettings);
            Assert.True(File.Exists(projectSettings.GetSettingsPath()));
        }
Esempio n. 6
0
        public IEnumerator SetServerPublicAccess()
        {
            EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);

            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();
            MeshSyncServer          mss             = MeshSyncMenu.CreateMeshSyncServer(true);

            Assert.IsTrue(mss.IsServerStarted());
            yield return(null);

            //Check the original public access
            bool origPublicAccess = projectSettings.GetServerPublicAccess();

            UnityEngine.Assertions.Assert.AreEqual(origPublicAccess, mss.DoesServerAllowPublicAccess());
            yield return(null);

            //Change the public access and check
            projectSettings.SetServerPublicAccess(!origPublicAccess);
            mss.StopServer();
            mss.StartServer();
            Assert.IsTrue(mss.IsServerStarted());
            yield return(null);

            UnityEngine.Assertions.Assert.AreEqual(projectSettings.GetServerPublicAccess(), mss.DoesServerAllowPublicAccess());

            //Change back
            projectSettings.SetServerPublicAccess(origPublicAccess);
            UnityEngine.Assertions.Assert.AreEqual(origPublicAccess, projectSettings.GetServerPublicAccess());
        }
Esempio n. 7
0
        public void CreateProjectSettings()
        {
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateInstance();

            Assert.NotNull(projectSettings);
            Assert.True(File.Exists(projectSettings.GetJsonPath()));
        }
Esempio n. 8
0
        public IEnumerator CheckServerSettings()
        {
            MeshSyncServer          mss             = MeshSyncMenu.CreateMeshSyncServer(true);
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateInstance();

            Assert.AreEqual(projectSettings.GetDefaultServerPort(), mss.GetServerPort());
            yield return(null);
        }
Esempio n. 9
0
//----------------------------------------------------------------------------------------------------------------------

        //Support Toggle, FloatField, etc
        private F AddField <F, V>(VisualElement parent, GUIContent content,
                                  V initialValue, Action <V> onValueChanged) where F : VisualElement, INotifyValueChanged <V>, new()
        {
            F field = UIElementsEditorUtility.AddField <F, V>(parent, content, initialValue, (ChangeEvent <V> changeEvent) => {
                onValueChanged(changeEvent.newValue);
                MeshSyncProjectSettings.GetOrCreateInstance().SaveInEditor();
            });

            field.AddToClassList("project-settings-field");
            return(field);
        }
Esempio n. 10
0
//----------------------------------------------------------------------------------------------------------------------    
    /// <inheritdoc/>
    public override void OnCreate(TimelineClip clip, TrackAsset track, TimelineClip clonedFrom) {
        
        SceneCachePlayableAsset asset = clip.asset as SceneCachePlayableAsset;
        if (null == asset) {
            Debug.LogError("[MeshSync] Asset is not a SceneCachePlayableAsset: " + clip.asset);
            return;
        }
        
        SceneCachePlayerConfig config = MeshSyncProjectSettings.GetOrCreateSettings().GetDefaultSceneCachePlayerConfig();        
        asset.SetSnapToFrame((SnapToFrame) config.TimelineSnapToFrame);

        //OnCreate() is called before the clip is assigned to the track, but we need the track for creating curves.
        clip.TryMoveToTrack(track);
                       
    }
Esempio n. 11
0
//----------------------------------------------------------------------------------------------------------------------

        private void UpdatePlayerConfigUIElements(MeshSyncPlayerType playerType)
        {
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();
            MeshSyncPlayerConfig    config          = projectSettings.GetDefaultPlayerConfig(playerType);

            //sync
            m_syncVisibilityToggle.SetValueWithoutNotify(config.SyncVisibility);
            m_syncTransformToggle.SetValueWithoutNotify(config.SyncTransform);
            m_syncCamerasToggle.SetValueWithoutNotify(config.SyncCameras);
            m_syncLightsToggle.SetValueWithoutNotify(config.SyncLights);
            m_syncMeshesToggle.SetValueWithoutNotify(config.SyncMeshes);
            m_updateMeshCollidersToggle.SetValueWithoutNotify(config.UpdateMeshColliders);
            m_syncMaterialsToggle.SetValueWithoutNotify(config.SyncMaterials);
            m_findMaterialFromAssetsToggle.SetValueWithoutNotify(config.FindMaterialFromAssets);

            //Import
            m_animationInterpolationPopup.SetValueWithoutNotify(m_animationInterpolationEnums[config.AnimationInterpolation]);
            m_keyframeReductionToggle.SetValueWithoutNotify(config.KeyframeReduction);
            m_reductionThresholdField.SetValueWithoutNotify(config.ReductionThreshold);
            m_reductionEraseFlatCurves.SetValueWithoutNotify(config.ReductionEraseFlatCurves);
            m_zUpCorrectionPopup.SetValueWithoutNotify(m_zUpCorrectionEnums[config.ZUpCorrection]);

            //Misc
            m_syncMaterialListToggle.SetValueWithoutNotify(config.SyncMaterialList);
            m_progressiveDisplayToggle.SetValueWithoutNotify(config.ProgressiveDisplay);
            m_loggingToggle.SetValueWithoutNotify(config.Logging);
            m_profilingToggle.SetValueWithoutNotify(config.Profiling);

            //Animation Tweak
            AnimationTweakSettings animationTweakSettings = config.GetAnimationTweakSettings();

            m_animationTweakTimeScaleField.SetValueWithoutNotify(animationTweakSettings.TimeScale);
            m_animationTweakTimeOffsetField.SetValueWithoutNotify(animationTweakSettings.TimeOffset);
            m_animationTweakDropStepField.SetValueWithoutNotify(animationTweakSettings.DropStep);
            m_animationTweakReductionThresholdField.SetValueWithoutNotify(animationTweakSettings.ReductionThreshold);
            m_animationTweakEraseFlatCurvesToggle.SetValueWithoutNotify(animationTweakSettings.EraseFlatCurves);

            //userData
            foreach (VisualElement uiElement in m_playerConfigUIElements)
            {
                uiElement.userData = config;
            }


            m_selectedPlayerType = playerType;
        }
Esempio n. 12
0
//----------------------------------------------------------------------------------------------------------------------    

    private static bool ValidateSceneCacheOutputPath() {
        
        MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();
        string                  scOutputPath    = projectSettings.GetSceneCacheOutputPath();
        if (!scOutputPath.StartsWith("Assets")) {
            DisplaySceneCacheOutputPathErrorDialog(scOutputPath);
            return false;            
        }        
        
        try {
            Directory.CreateDirectory(scOutputPath);
        } catch {
            DisplaySceneCacheOutputPathErrorDialog(scOutputPath);
            return false;
        }

        return true;
    }
//----------------------------------------------------------------------------------------------------------------------

        //Support Toggle, FloatField, etc
        private F AddPlayerConfigField <F, V>(VisualElement parent, GUIContent content, V initialValue,
                                              Action <V> onValueChanged, string containerClass = null) where F : VisualElement, INotifyValueChanged <V>, new()
        {
            F field = UIElementsEditorUtility.AddField <F, V>(parent, content, initialValue, (ChangeEvent <V> changeEvent) => {
                F targetField = (changeEvent.target) as F;
                if (null == targetField)
                {
                    return;
                }

                onValueChanged(changeEvent.newValue);
                MeshSyncProjectSettings.GetOrCreateInstance().SaveInEditor();
            });

            field.AddToClassList("general-settings-field");
            if (!string.IsNullOrEmpty(containerClass))
            {
                field.parent.AddToClassList(containerClass);
            }

            return(field);
        }
Esempio n. 14
0
//----------------------------------------------------------------------------------------------------------------------
        public void Setup(VisualElement root)
        {
            Assert.IsNotNull(root);
            root.Clear();

            VisualTreeAsset   tab         = UIElementsEditorUtility.LoadVisualTreeAsset(Constants.SERVER_SETTINGS_TAB_PATH);
            TemplateContainer tabInstance = tab.CloneTree();

            VisualElement content = tabInstance.Query <VisualElement>("Content").First();

            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateInstance();

            //Add server port
            m_serverPortField = AddField <IntegerField, int>(content, Contents.ServerPort,
                                                             projectSettings.GetDefaultServerPort(),
                                                             (int newValue) => { projectSettings.SetDefaultServerPort((ushort)newValue); }
                                                             );

            m_allowPublicAccessToggle = AddField <Toggle, bool>(content, Contents.AllowPublicAccess,
                                                                projectSettings.GetServerPublicAccess(),
                                                                (bool newValue) => { projectSettings.SetServerPublicAccess(newValue); }
                                                                );

            //MeshSyncPlayerConfig section
            MeshSyncPlayerConfigSection section = new MeshSyncPlayerConfigSection(MeshSyncPlayerType.SERVER);

            section.Setup(content);

            Button resetButton = tabInstance.Query <Button>("ResetButton").First();

            resetButton.clicked += () => {
                projectSettings.ResetDefaultServerConfig();
                projectSettings.SaveInEditor();
                Setup(root);
            };


            root.Add(tabInstance);
        }
//----------------------------------------------------------------------------------------------------------------------
        public void Setup(VisualElement root)
        {
            Assert.IsNotNull(root);
            root.Clear();

            VisualTreeAsset   tab         = UIElementsEditorUtility.LoadVisualTreeAsset(Constants.SCENE_CACHE_PLAYER_SETTINGS_TAB_PATH);
            TemplateContainer tabInstance = tab.CloneTree();

            VisualElement content = tabInstance.Query <VisualElement>("Content").First();

            m_generatedSCResPathTextField = tabInstance.Query <TextField>("GeneratedSCResPathText").First();
            m_generatedSCResPathTextField.RegisterValueChangedCallback((ChangeEvent <string> changeEvent) => {
                MeshSyncProjectSettings settings = MeshSyncProjectSettings.GetOrCreateSettings();
                settings.SetSceneCacheOutputPath(changeEvent.newValue);
                settings.Save();
            });

            m_outputPathSelectButton          = tabInstance.Query <Button>("OutputPathSelectButton").First();
            m_outputPathSelectButton.clicked += OnOutputPathSelectButtonClicked;
            RefreshSettings();

            //MeshSyncPlayerConfig
            MeshSyncPlayerConfigSection section = new MeshSyncPlayerConfigSection(MeshSyncPlayerType.CACHE_PLAYER);

            section.Setup(content);

            Button resetButton = tabInstance.Query <Button>("ResetButton").First();

            resetButton.clicked += () => {
                MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();
                projectSettings.ResetDefaultSceneCachePlayerConfig();
                projectSettings.Save();
                Setup(root);
            };


            root.Add(tabInstance);
        }
//----------------------------------------------------------------------------------------------------------------------
        private PopupField <T> AddPlayerConfigPopupField <T>(VisualElement parent, GUIContent content,
                                                             List <T> options, T initialValue, Action <int> onValueChanged,
                                                             string containerClass = null)
        {
            PopupField <T> popupField = UIElementsEditorUtility.AddPopupField <T>(parent, content, options, initialValue,
                                                                                  (ChangeEvent <T> changeEvent) => {
                PopupField <T> targetField = (changeEvent.target) as PopupField <T>;
                if (null == targetField)
                {
                    return;
                }

                onValueChanged(targetField.index);
                MeshSyncProjectSettings.GetOrCreateInstance().SaveInEditor();
            }
                                                                                  );

            popupField.AddToClassList("general-settings-field");
            if (!string.IsNullOrEmpty(containerClass))
            {
                popupField.parent.AddToClassList(containerClass);
            }
            return(popupField);
        }
Esempio n. 17
0
        private void Reset()
        {
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();

            m_importerSettings = projectSettings.GetDefaultSceneCachePlayerConfig().GetModelImporterSettings();
        }
//----------------------------------------------------------------------------------------------------------------------
        internal void Setup(VisualElement parent)
        {
            bool isSceneCachePlayerConfig           = (m_playerType == MeshSyncPlayerType.CACHE_PLAYER);
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateInstance();
            MeshSyncPlayerConfig    config          = null;

            config = isSceneCachePlayerConfig
            ? (MeshSyncPlayerConfig)projectSettings.GetDefaultSceneCachePlayerConfig()
            : projectSettings.GetDefaultServerConfig();


            TemplateContainer containerInstance = InstantiateContainer(m_playerType);

            parent.Add(containerInstance);

            //Add server port
            Foldout syncSettingsFoldout = containerInstance.Query <Foldout>("SyncSettingsFoldout").First();

            //Sync
            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.UpdateTransform, config.SyncTransform,
                                                (bool newValue) => { config.SyncTransform = newValue; }
                                                );

            {
                int i = MeshSyncPlayerConfig.SYNC_CAMERA;
                ComponentSyncSettings componentSyncSettings = config.GetComponentSyncSettings(i);
                AddComponentSyncSettingFields(syncSettingsFoldout, Contents.ComponentSyncs[i], componentSyncSettings);
            }

            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.UsePhysicalCameraParams, config.IsPhysicalCameraParamsUsed(),
                                                (bool newValue) => { config.UsePhysicalCameraParams(newValue); },
                                                "inner-field-container"
                                                );

            {
                int i = MeshSyncPlayerConfig.SYNC_LIGHTS;
                ComponentSyncSettings componentSyncSettings = config.GetComponentSyncSettings(i);
                AddComponentSyncSettingFields(syncSettingsFoldout, Contents.ComponentSyncs[i], componentSyncSettings);
            }


            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.Meshes, config.SyncMeshes,
                                                (bool newValue) => { config.SyncMeshes = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.UpdateMeshColliders, config.UpdateMeshColliders,
                                                (bool newValue) => { config.UpdateMeshColliders = newValue; },
                                                "inner-field-container"
                                                );

            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.Visibility, config.SyncVisibility,
                                                (bool newValue) => { config.SyncVisibility = newValue; }
                                                );

            //import
            Foldout importSettingsFoldout = containerInstance.Query <Foldout>("ImportSettingsFoldout").First();
            ModelImporterSettings modelImporterSettings = config.GetModelImporterSettings();

            AddPlayerConfigField <Toggle, bool>(importSettingsFoldout,
                                                Contents.CreateMaterials, modelImporterSettings.CreateMaterials,
                                                (bool newValue) => { modelImporterSettings.CreateMaterials = newValue; }
                                                );
            AddPlayerConfigPopupField(importSettingsFoldout, Contents.MaterialSearchMode, m_assetSearchModeEnums,
                                      m_assetSearchModeEnums[(int)modelImporterSettings.MaterialSearchMode],
                                      (int newValue) => { modelImporterSettings.MaterialSearchMode = (AssetSearchMode)newValue; },
                                      "inner-field-container"
                                      );

            AddPlayerConfigPopupField(importSettingsFoldout, Contents.AnimationInterpolation, m_animationInterpolationEnums,
                                      m_animationInterpolationEnums[config.AnimationInterpolation],
                                      (int newValue) => { config.AnimationInterpolation = newValue; }
                                      );
            AddPlayerConfigField <Toggle, bool>(importSettingsFoldout, Contents.KeyframeReduction, config.KeyframeReduction,
                                                (bool newValue) => { config.KeyframeReduction = newValue; }
                                                );
            AddPlayerConfigField <FloatField, float>(importSettingsFoldout,
                                                     Contents.ReductionThreshold, config.ReductionThreshold,
                                                     (float newValue) => { config.ReductionThreshold = newValue; }
                                                     );
            AddPlayerConfigField <Toggle, bool>(importSettingsFoldout,
                                                Contents.ReductionEraseFlatCurves, config.ReductionEraseFlatCurves,
                                                (bool newValue) => { config.ReductionEraseFlatCurves = newValue; }
                                                );
            AddPlayerConfigPopupField(importSettingsFoldout,
                                      Contents.ZUpCorrection, m_zUpCorrectionEnums, m_zUpCorrectionEnums[config.ZUpCorrection],
                                      (int newValue) => { config.ZUpCorrection = newValue; }
                                      );

            //Misc
            Foldout miscSettingsFoldout = containerInstance.Query <Foldout>("MiscSettingsFoldout").First();

            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout,
                                                Contents.SyncMaterialList, config.SyncMaterialList,
                                                (bool newValue) => { config.SyncMaterialList = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout, Contents.ProgressiveDisplay, config.ProgressiveDisplay,
                                                (bool newValue) => { config.ProgressiveDisplay = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout, Contents.Logging, config.Logging,
                                                (bool newValue) => { config.Logging = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout, Contents.Profiling, config.Profiling,
                                                (bool newValue) => { config.Profiling = newValue; }
                                                );


            if (!isSceneCachePlayerConfig)
            {
                return;
            }
        }
//----------------------------------------------------------------------------------------------------------------------

        void RefreshSettings()
        {
            MeshSyncProjectSettings settings = MeshSyncProjectSettings.GetOrCreateSettings();

            m_generatedSCResPathTextField.value = settings.GetSceneCacheOutputPath();
        }
Esempio n. 20
0
//----------------------------------------------------------------------------------------------------------------------    
    
    internal static void ChangeSceneCacheFile(SceneCachePlayer cachePlayer, string sceneCacheFilePath) {
        string     prefabPath = null;
        GameObject go         = cachePlayer.gameObject;
        //Check if it's possible to reuse the old assetsFolder
        string assetsFolder = cachePlayer.GetAssetsFolder();
        if (string.IsNullOrEmpty(assetsFolder)) {
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();        
            string                  scOutputPath    = projectSettings.GetSceneCacheOutputPath();            
            assetsFolder = Path.Combine(scOutputPath, Path.GetFileNameWithoutExtension(sceneCacheFilePath));
        }
        
        bool isPrefabInstance = cachePlayer.gameObject.IsPrefabInstance();        
        //We are directly modifying a prefab
        if (!isPrefabInstance && go.IsPrefab()) {
            prefabPath = AssetDatabase.GetAssetPath(go);
            CreateSceneCachePlayerAndPrefab(sceneCacheFilePath, prefabPath, assetsFolder, out SceneCachePlayer player,
                out GameObject newPrefab);
            Object.DestroyImmediate(player.gameObject);
            return;

        } 
        
        if (isPrefabInstance) {
            GameObject prefab = PrefabUtility.GetCorrespondingObjectFromSource(cachePlayer.gameObject);
            
            //GetCorrespondingObjectFromSource() may return the ".sc" GameObject instead of the prefab
            //due to the SceneCacheImporter
            string assetPath = AssetDatabase.GetAssetPath(prefab);
            if (Path.GetExtension(assetPath).ToLower() == ".prefab") {
                prefabPath = assetPath;
            } else {
                isPrefabInstance = false;
            }            
        } 
        
        cachePlayer.CloseCache();
        
        //[TODO-sin: 2020-9-28] Find out if it is possible to do undo properly
        Undo.RegisterFullObjectHierarchyUndo(cachePlayer.gameObject, "SceneCachePlayer");
        
        Dictionary<string,EntityRecord> prevRecords = new Dictionary<string, EntityRecord>(cachePlayer.GetClientObjects());        
        
        GameObject tempGO = null;
        Dictionary<Transform, Transform> nonPrefabTrans = new Dictionary<Transform, Transform>(); //nonPrefab -> origParent
        if (isPrefabInstance) {
            //Move non-prefab transforms
            tempGO = new GameObject("Temp");
            FindNonPrefabChildren(cachePlayer.transform, ref nonPrefabTrans);
            nonPrefabTrans.Keys.SetParent(tempGO.transform);
            
            PrefabUtility.UnpackPrefabInstance(cachePlayer.gameObject, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);                       
        }


        //remove irrelevant  components of the GameObject if the entity type is different
        void ChangeEntityTypeCB(GameObject updatedGo, TransformData data) {
            string dataPath = data.path;
            if (!prevRecords.ContainsKey(dataPath)) 
                return;

            EntityRecord prevRecord = prevRecords[dataPath];
            if (data.entityType == prevRecord.dataType) 
                return;

            DestroyIrrelevantComponents(updatedGo, data.entityType);
        }


        cachePlayer.onUpdateEntity += ChangeEntityTypeCB;        
        cachePlayer.Init(assetsFolder);
        cachePlayer.OpenCacheInEditor(sceneCacheFilePath);        
        cachePlayer.onUpdateEntity -= ChangeEntityTypeCB;
        
        IDictionary<string,EntityRecord> curRecords = cachePlayer.GetClientObjects();
        DeleteInvalidRecordedGameObjects(prevRecords, curRecords);

        if (!isPrefabInstance) {
            return;
        }
        
        
        cachePlayer.gameObject.SaveAsPrefab(prefabPath); //Save as prefab if it was originally a prefab instance
        
        //Move nonPrefab transforms back
        foreach (KeyValuePair<Transform, Transform> kv in nonPrefabTrans) {
            Transform origParent = kv.Value;
            Transform t          = kv.Key;
            if (null == origParent) {
                ObjectUtility.Destroy(t.gameObject);
            } else {
                t.SetParent(origParent);
            } 
        }
        ObjectUtility.Destroy(tempGO);
        
        
    }
Esempio n. 21
0
//----------------------------------------------------------------------------------------------------------------------
        public void Setup(VisualElement root)
        {
            m_playerConfigUIElements = new List <VisualElement>();
            VisualTreeAsset container = UIElementsEditorUtility.LoadVisualTreeAsset(
                Path.Combine(MeshSyncEditorConstants.PROJECT_SETTINGS_UIELEMENTS_PATH, "GeneralSettings_Container")
                );
            TemplateContainer containerInstance = container.CloneTree();


            List <string> objectTypes = new List <string> {
                MeshSyncPlayerType.SERVER.ToString(),
                          MeshSyncPlayerType.CACHE_PLAYER.ToString(),
            };

            //Templates
            VisualTreeAsset fieldTemplate = UIElementsEditorUtility.LoadVisualTreeAsset(
                Path.Combine(MeshSyncEditorConstants.PROJECT_SETTINGS_UIELEMENTS_PATH, "GeneralSettingsFieldTemplate")
                );

            //Add server port
            MeshSyncProjectSettings projectSettings = MeshSyncProjectSettings.GetOrCreateSettings();
            VisualElement           headerContainer = containerInstance.Query <VisualElement>("HeaderContainer").First();

            m_serverPortField = new IntegerField("Server Port");
            m_serverPortField.SetValueWithoutNotify(projectSettings.GetDefaultServerPort());

            m_serverPortField.RegisterValueChangedCallback((ChangeEvent <int> evt) => {
                MeshSyncProjectSettings settings = MeshSyncProjectSettings.GetOrCreateSettings();
                settings.SetDefaultServerPort((ushort)evt.newValue);
                settings.SaveSettings();
            });
            headerContainer.Add(m_serverPortField);

            //Add playerType popup
            VisualElement       playerTypePopupContainer = containerInstance.Query <VisualElement>("PlayerTypePopupContainer").First();
            PopupField <string> playerTypePopup          = new PopupField <string>("Settings for object type", objectTypes, objectTypes[0]);

            playerTypePopup.RegisterValueChangedCallback(OnPlayerTypePopupChanged);
            playerTypePopupContainer.Add(playerTypePopup);


            Foldout syncSettingsFoldout = containerInstance.Query <Foldout>("SyncSettingsFoldout").First();

            //Sync
            m_syncVisibilityToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout, "Visibility",
                                                             (MeshSyncPlayerConfig config, bool newValue) => { config.SyncVisibility = newValue; }
                                                             );
            m_syncTransformToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout, "Transform",
                                                            (MeshSyncPlayerConfig config, bool newValue) => { config.SyncTransform = newValue; }
                                                            );
            m_syncCamerasToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout, "Cameras",
                                                          (MeshSyncPlayerConfig config, bool newValue) => { config.SyncCameras = newValue; }
                                                          );
            m_syncLightsToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout, "Lights",
                                                         (MeshSyncPlayerConfig config, bool newValue) => { config.SyncLights = newValue; }
                                                         );
            m_syncMeshesToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout, "Meshes",
                                                         (MeshSyncPlayerConfig config, bool newValue) => { config.SyncMeshes = newValue; }
                                                         );
            m_updateMeshCollidersToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout,
                                                                  "Update Mesh Colliders",
                                                                  (MeshSyncPlayerConfig config, bool newValue) => { config.UpdateMeshColliders = newValue; }
                                                                  );
            m_syncMaterialsToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout, "Materials",
                                                            (MeshSyncPlayerConfig config, bool newValue) => { config.SyncMaterials = newValue; }
                                                            );
            m_findMaterialFromAssetsToggle = AddField <Toggle, bool>(fieldTemplate, syncSettingsFoldout,
                                                                     "Find Materials from Asset Database",
                                                                     (MeshSyncPlayerConfig config, bool newValue) => { config.FindMaterialFromAssets = newValue; }
                                                                     );

            //import
            Foldout importSettingsFoldout = containerInstance.Query <Foldout>("ImportSettingsFoldout").First();

            m_animationInterpolationPopup = AddPopupField(fieldTemplate, importSettingsFoldout,
                                                          "Animation Interpolation", m_animationInterpolationEnums,
                                                          (MeshSyncPlayerConfig config, int newValue) => { config.AnimationInterpolation = newValue; }
                                                          );
            m_keyframeReductionToggle = AddField <Toggle, bool>(fieldTemplate, importSettingsFoldout, "Keyframe Reduction",
                                                                (MeshSyncPlayerConfig config, bool newValue) => { config.KeyframeReduction = newValue; }
                                                                );
            m_reductionThresholdField = AddField <FloatField, float>(fieldTemplate, importSettingsFoldout,
                                                                     "Reduction Threshold",
                                                                     (MeshSyncPlayerConfig config, float newValue) => { config.ReductionThreshold = newValue; }
                                                                     );
            m_reductionEraseFlatCurves = AddField <Toggle, bool>(fieldTemplate, importSettingsFoldout,
                                                                 "Reduction Erase Flat Curves",
                                                                 (MeshSyncPlayerConfig config, bool newValue) => { config.ReductionEraseFlatCurves = newValue; }
                                                                 );
            m_zUpCorrectionPopup = AddPopupField(fieldTemplate, importSettingsFoldout,
                                                 "Z-Up correction", m_zUpCorrectionEnums,
                                                 (MeshSyncPlayerConfig config, int newValue) => { config.ZUpCorrection = newValue; }
                                                 );

            //Misc
            Foldout miscSettingsFoldout = containerInstance.Query <Foldout>("MiscSettingsFoldout").First();

            m_syncMaterialListToggle = AddField <Toggle, bool>(fieldTemplate, miscSettingsFoldout,
                                                               "Sync Material List",
                                                               (MeshSyncPlayerConfig config, bool newValue) => { config.SyncMaterialList = newValue; }
                                                               );
            m_progressiveDisplayToggle = AddField <Toggle, bool>(fieldTemplate, miscSettingsFoldout,
                                                                 "Progressive Display",
                                                                 (MeshSyncPlayerConfig config, bool newValue) => { config.ProgressiveDisplay = newValue; }
                                                                 );
            m_loggingToggle = AddField <Toggle, bool>(fieldTemplate, miscSettingsFoldout,
                                                      "Logging",
                                                      (MeshSyncPlayerConfig config, bool newValue) => { config.Logging = newValue; }
                                                      );
            m_profilingToggle = AddField <Toggle, bool>(fieldTemplate, miscSettingsFoldout,
                                                        "Profiling",
                                                        (MeshSyncPlayerConfig config, bool newValue) => { config.Profiling = newValue; }
                                                        );

            //Animation Tweak
            Foldout animationTweakSettingsFoldout = containerInstance.Query <Foldout>("AnimationTweakSettingsFoldout").First();

            m_animationTweakTimeScaleField = AddField <FloatField, float>(fieldTemplate, animationTweakSettingsFoldout,
                                                                          "Time Scale",
                                                                          (MeshSyncPlayerConfig config, float newValue) => {
                config.GetAnimationTweakSettings().TimeScale = newValue;
            }
                                                                          );
            m_animationTweakTimeOffsetField = AddField <FloatField, float>(fieldTemplate, animationTweakSettingsFoldout,
                                                                           "Time Offset",
                                                                           (MeshSyncPlayerConfig config, float newValue) => {
                config.GetAnimationTweakSettings().TimeOffset = newValue;
            }
                                                                           );
            m_animationTweakDropStepField = AddField <IntegerField, int>(fieldTemplate, animationTweakSettingsFoldout,
                                                                         "Drop Step",
                                                                         (MeshSyncPlayerConfig config, int newValue) => {
                config.GetAnimationTweakSettings().DropStep = newValue;
            }
                                                                         );
            m_animationTweakReductionThresholdField = AddField <FloatField, float>(fieldTemplate, animationTweakSettingsFoldout,
                                                                                   "Reduction Threshold",
                                                                                   (MeshSyncPlayerConfig config, float newValue) => {
                config.GetAnimationTweakSettings().ReductionThreshold = newValue;
            }
                                                                                   );
            m_animationTweakEraseFlatCurvesToggle = AddField <Toggle, bool>(fieldTemplate, animationTweakSettingsFoldout,
                                                                            "Erase Flat Curves",
                                                                            (MeshSyncPlayerConfig config, bool newValue) => {
                config.GetAnimationTweakSettings().EraseFlatCurves = newValue;
            }
                                                                            );

            UpdatePlayerConfigUIElements(MeshSyncPlayerType.SERVER);

            root.Add(containerInstance);
        }
Esempio n. 22
0
//----------------------------------------------------------------------------------------------------------------------
        internal void Setup(VisualElement parent)
        {
            bool isSceneCachePlayerConfig = (m_playerType == MeshSyncPlayerType.CACHE_PLAYER);
            MeshSyncPlayerConfig config   = null;

            if (isSceneCachePlayerConfig)
            {
                config = MeshSyncProjectSettings.GetOrCreateSettings().GetDefaultSceneCachePlayerConfig();
            }
            else
            {
                config = MeshSyncProjectSettings.GetOrCreateSettings().GetDefaultServerConfig();
            }


            TemplateContainer containerInstance = InstantiateContainer(m_playerType);

            parent.Add(containerInstance);

            //Add server port
            Foldout syncSettingsFoldout = containerInstance.Query <Foldout>("SyncSettingsFoldout").First();

            //Sync
            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.UpdateTransform, config.SyncTransform,
                                                (bool newValue) => { config.SyncTransform = newValue; }
                                                );

            {
                int i = MeshSyncPlayerConfig.SYNC_CAMERA;
                ComponentSyncSettings componentSyncSettings = config.GetComponentSyncSettings(i);
                AddComponentSyncSettingFields(syncSettingsFoldout, Contents.ComponentSyncs[i], componentSyncSettings);
            }

            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.UsePhysicalCameraParams, config.IsPhysicalCameraParamsUsed(),
                                                (bool newValue) => { config.UsePhysicalCameraParams(newValue); },
                                                "inner-field-container"
                                                );

            {
                int i = MeshSyncPlayerConfig.SYNC_LIGHTS;
                ComponentSyncSettings componentSyncSettings = config.GetComponentSyncSettings(i);
                AddComponentSyncSettingFields(syncSettingsFoldout, Contents.ComponentSyncs[i], componentSyncSettings);
            }


            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.Meshes, config.SyncMeshes,
                                                (bool newValue) => { config.SyncMeshes = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.UpdateMeshColliders, config.UpdateMeshColliders,
                                                (bool newValue) => { config.UpdateMeshColliders = newValue; },
                                                "inner-field-container"
                                                );

            AddPlayerConfigField <Toggle, bool>(syncSettingsFoldout, Contents.Visibility, config.SyncVisibility,
                                                (bool newValue) => { config.SyncVisibility = newValue; }
                                                );

            //import
            Foldout importSettingsFoldout = containerInstance.Query <Foldout>("ImportSettingsFoldout").First();
            ModelImporterSettings modelImporterSettings = config.GetModelImporterSettings();

            AddPlayerConfigField <Toggle, bool>(importSettingsFoldout,
                                                Contents.CreateMaterials, modelImporterSettings.CreateMaterials,
                                                (bool newValue) => { modelImporterSettings.CreateMaterials = newValue; }
                                                );
            AddPlayerConfigPopupField(importSettingsFoldout, Contents.MaterialSearchMode, m_assetSearchModeEnums,
                                      m_assetSearchModeEnums[(int)modelImporterSettings.MaterialSearchMode],
                                      (int newValue) => { modelImporterSettings.MaterialSearchMode = (AssetSearchMode)newValue; },
                                      "inner-field-container"
                                      );

            AddPlayerConfigPopupField(importSettingsFoldout, Contents.AnimationInterpolation, m_animationInterpolationEnums,
                                      m_animationInterpolationEnums[config.AnimationInterpolation],
                                      (int newValue) => { config.AnimationInterpolation = newValue; }
                                      );
            AddPlayerConfigField <Toggle, bool>(importSettingsFoldout, Contents.KeyframeReduction, config.KeyframeReduction,
                                                (bool newValue) => { config.KeyframeReduction = newValue; }
                                                );
            AddPlayerConfigField <FloatField, float>(importSettingsFoldout,
                                                     Contents.ReductionThreshold, config.ReductionThreshold,
                                                     (float newValue) => { config.ReductionThreshold = newValue; }
                                                     );
            AddPlayerConfigField <Toggle, bool>(importSettingsFoldout,
                                                Contents.ReductionEraseFlatCurves, config.ReductionEraseFlatCurves,
                                                (bool newValue) => { config.ReductionEraseFlatCurves = newValue; }
                                                );
            AddPlayerConfigPopupField(importSettingsFoldout,
                                      Contents.ZUpCorrection, m_zUpCorrectionEnums, m_zUpCorrectionEnums[config.ZUpCorrection],
                                      (int newValue) => { config.ZUpCorrection = newValue; }
                                      );

            //Misc
            Foldout miscSettingsFoldout = containerInstance.Query <Foldout>("MiscSettingsFoldout").First();

            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout,
                                                Contents.SyncMaterialList, config.SyncMaterialList,
                                                (bool newValue) => { config.SyncMaterialList = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout, Contents.ProgressiveDisplay, config.ProgressiveDisplay,
                                                (bool newValue) => { config.ProgressiveDisplay = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout, Contents.Logging, config.Logging,
                                                (bool newValue) => { config.Logging = newValue; }
                                                );
            AddPlayerConfigField <Toggle, bool>(miscSettingsFoldout, Contents.Profiling, config.Profiling,
                                                (bool newValue) => { config.Profiling = newValue; }
                                                );

            //Animation Tweak
            Foldout atsFoldout         = containerInstance.Query <Foldout>("AnimationTweakSettingsFoldout").First();
            AnimationTweakSettings ats = config.GetAnimationTweakSettings();

            AddPlayerConfigField <FloatField, float>(atsFoldout, Contents.TweakTimeScale, ats.TimeScale,
                                                     (float newValue) => { ats.TimeScale = newValue; }
                                                     );
            AddPlayerConfigField <FloatField, float>(atsFoldout, Contents.TweakTimeOffset, ats.TimeOffset,
                                                     (float newValue) => { ats.TimeOffset = newValue; }
                                                     );
            AddPlayerConfigField <IntegerField, int>(atsFoldout, Contents.TweakDropStep, ats.DropStep,
                                                     (int newValue) => { ats.DropStep = newValue; }
                                                     );
            AddPlayerConfigField <FloatField, float>(atsFoldout, Contents.TweakReductionThreshold, ats.ReductionThreshold,
                                                     (float newValue) => { ats.ReductionThreshold = newValue; }
                                                     );
            AddPlayerConfigField <Toggle, bool>(atsFoldout, Contents.TweakEraseFlatCurves, ats.EraseFlatCurves,
                                                (bool newValue) => { ats.EraseFlatCurves = newValue; }
                                                );

            if (!isSceneCachePlayerConfig)
            {
                return;
            }

            //Additional UI for SceneCache
            SceneCachePlayerConfig scPlayerConfig = config as SceneCachePlayerConfig;

            Assert.IsNotNull(scPlayerConfig);
            Foldout timelineSettingsFoldout = containerInstance.Query <Foldout>("TimelineSettingsFoldout").First();

            AddPlayerConfigPopupField(timelineSettingsFoldout,
                                      Contents.TimelineSnapToFrame, m_snapToFrameEnums, m_snapToFrameEnums[scPlayerConfig.TimelineSnapToFrame],
                                      (int newValue) => { scPlayerConfig.TimelineSnapToFrame = newValue; }
                                      );
        }