/// <summary>
 /// if this property is in a DynamicDNAPlugin this will find it so we can use its DNAAsset etc
 /// </summary>
 /// <param name="property"></param>
 private void CheckDynamicDNAPlugin(SerializedProperty property)
 {
     if (typeof(DynamicDNAPlugin).IsAssignableFrom((property.serializedObject.targetObject).GetType()))
     {
         _dynamicDNAPlugin = (DynamicDNAPlugin)property.serializedObject.targetObject;
     }
 }
예제 #2
0
        /// <summary>
        /// Upgrades a DynamicDNAConverter Prefab to a new ConverterController asset and replaces any usage of the old asset. Stores the original asset in a relative 'Legacy' folder.
        /// </summary>
        /// <returns>Returns the path of the new converterController asset</returns>
        public DynamicDNAConverterController DoUpgrade()
        {
            var DCBPath = AssetDatabase.GetAssetPath(this.gameObject);

            //In Unity 2018.3+ this asset may be being inspected in its own Prefab scene (rather than via customizer).
            //If that is the case we need to get the path differently
#if UNITY_2018_3_OR_NEWER
            var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(this.gameObject);
            if (prefabStage != null)
            {
                DCBPath = prefabStage.prefabAssetPath;
            }
#endif
            if (string.IsNullOrEmpty(DCBPath))
            {
                Debug.LogWarning("Upgrade could not be completed. Could not get asset path for the DNAConverterBehaviour to upgrade");
                return(null);
            }

            var newControllerName = this.name.Replace("DynamicDNAConverterBehaviour", "").Replace("DynamicDNAConverter", "").Replace("DNAConverterBehaviour", "").Replace("ConverterBehaviour", "").Replace("Legacy", "");
            if (_converterController != null)
            {
                Debug.LogWarning("Upgrading " + this.gameObject.name + " failed because it already references a previously converted version. If you need to Upgrade again please inspect this assets 'Legacy Settings' and click 'Revert To Legacy Settings'");
                return(null);
            }

            DynamicDNAConverterController newController = null;
            DynamicDNAPlugin skelModsPlug     = null;
            DynamicDNAPlugin startingPosePlug = null;

            newControllerName += "DNAConverterController";
            var path = DCBPath;
            path = path.Replace("/" + Path.GetFileName(path), "");
            var assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + newControllerName + ".asset");
            newController = DynamicDNAConverterController.CreateDynamicDNAConverterControllerAsset(assetPathAndName, false);
            if (newController == null)
            {
                //bail if the converterController was not created
                Debug.LogWarning("DynamicDNAConverterBehaviour BackupAndUpgrade failed because it was unable to create the new ConverterController.");
                return(null);
            }
            //Add skeletonModifiers
            if (_skeletonModifiers.Count > 0)
            {
                skelModsPlug = newController.AddPlugin(typeof(SkeletonDNAConverterPlugin));
                if (!((SkeletonDNAConverterPlugin)skelModsPlug).ImportSettings(this.gameObject, 0))
                {
                    Debug.LogWarning("Your SkeletonModifiers did not import correctly into the new plugin. Please try importing then manually");
                }
            }
            //Add startingPose
            if (_startingPose != null)
            {
                startingPosePlug = newController.AddPlugin(typeof(BonePoseDNAConverterPlugin));
                if (((BonePoseDNAConverterPlugin)startingPosePlug).ImportSettings(this.gameObject, 0))
                {
                    Debug.LogWarning("Your StartingPose did not import correctly into the new plugin. Please try importing it manually");
                }
            }
            //Import the rest of our data
            newController.ImportConverterBehaviourData(this);

            //Set this last because the backwards compatible public properties get values from it if its set
            _converterController = newController;

            EditorUtility.SetDirty(newController);
            if (skelModsPlug != null)
            {
                EditorUtility.SetDirty(skelModsPlug);
            }
            if (startingPosePlug != null)
            {
                EditorUtility.SetDirty(startingPosePlug);
            }

            //Find and replace the usage of this
            FindAndReplaceUsage(newController);

            //If this asset is not inside a 'LegacyDNA' folder move it inside one
            //We need to keep the old one because downloaded content may still require it
            //The RaceDatas and SlotDataAssets will warn the user if they are using a legacy DynamicDNAConverterBehaviour
            var    DCBFilename     = System.IO.Path.GetFileName(DCBPath);
            string moveAssetResult = "";
#pragma warning disable 0219
            string newDCBPath = DCBPath;
#pragma warning restore
            if (DCBPath.IndexOf("LegacyDNA" + "/" + DCBFilename) == -1)
            {
                var DCBDir = System.IO.Path.GetDirectoryName(DCBPath);
                if (!AssetDatabase.IsValidFolder(DCBDir + "/" + "LegacyDNA"))
                {
                    AssetDatabase.CreateFolder(DCBDir, "LegacyDNA");
                }
                if (DCBFilename.IndexOf("Legacy") == -1)
                {
                    DCBFilename = System.IO.Path.GetFileNameWithoutExtension(DCBPath) + " Legacy" + System.IO.Path.GetExtension(DCBPath);
                }
                moveAssetResult = AssetDatabase.MoveAsset(DCBPath, DCBDir + "/" + "LegacyDNA" + "/" + DCBFilename);
                if (string.IsNullOrEmpty(moveAssetResult))
                {
                    newDCBPath = DCBDir + "/" + "LegacyDNA" + "/" + DCBFilename;
                }
            }
            if (!string.IsNullOrEmpty(moveAssetResult))
            {
                Debug.LogWarning(moveAssetResult);
            }

#if UNITY_2018_3_OR_NEWER
            //If this happenned in a prefab stage (rather than via customizer) it wont save the prefab with the added converterController so
            if (prefabStage != null)
            {
                PrefabUtility.SaveAsPrefabAsset(this.gameObject, newDCBPath);
            }
#endif

            EditorUtility.SetDirty(this.gameObject);
            AssetDatabase.SaveAssets();
            return(_converterController);
        }
        //Draws a popup showing the available plugins for the project
        private void DrawAddConverterPopup(Rect position)
        {
            var ROLDefaults = new ReorderableList.Defaults();
            var padding     = 4f;

            _availablePlugins = DynamicDNAPlugin.GetAvailablePluginTypes();

            Rect addRect = Rect.zero;

            if (position == Rect.zero)
            {
                GUILayout.BeginVertical(_pluginChooserAreaStyle);
                addRect = EditorGUILayout.GetControlRect();
            }
            else
            {
                addRect = position;
            }
            addRect.xMin = addRect.xMax - 190 > addRect.xMin ? addRect.xMax - 190 : addRect.xMin;
            var labelRect    = new Rect(addRect.xMin + (padding * 2), addRect.yMin, addRect.width - (padding * 2), 0);
            var addPopupRect = new Rect(addRect.xMin + (padding * 2), labelRect.yMax, addRect.width - _addPluginBtnWidth - (padding * 2), EditorGUIUtility.singleLineHeight);
            var addBtnRect   = new Rect(addPopupRect.xMax + padding, labelRect.yMax, _addPluginBtnWidth - (padding * 3), EditorGUIUtility.singleLineHeight);

            if (Event.current.type == EventType.Repaint)
            {
                var prevFooterFixedHeight = ROLDefaults.footerBackground.fixedHeight;
                ROLDefaults.footerBackground.fixedHeight = addRect.height;
                ROLDefaults.footerBackground.Draw(addRect, false, false, false, false);
                ROLDefaults.footerBackground.fixedHeight = prevFooterFixedHeight;
            }

            var dropdownLabel = _pluginToAdd != null ? _pluginToAdd.Name : "Add Converters...";

            if (EditorGUI.DropdownButton(addPopupRect, new GUIContent(dropdownLabel, "Add converters of the selected type to the " + _dnaConvertersLabel + " list"), FocusType.Keyboard))
            {
                // create the menu and add items to it
                GenericMenu popupMenu = new GenericMenu();

                //add the choose entry- clears the selection
                AddMenuItemForAddConvertersPopup(popupMenu, null);

                //add the actual entries
                for (int i = 0; i < _availablePlugins.Count; i++)
                {
                    AddMenuItemForAddConvertersPopup(popupMenu, _availablePlugins[i]);
                }

                // display the menu
                popupMenu.DropDown(addPopupRect);
            }

            EditorGUI.BeginDisabledGroup(_pluginToAdd == null);
            if (GUI.Button(addBtnRect, new GUIContent("Add", (_pluginToAdd == null ? "Choose converters to add first" : ""))))
            {
                //do it!
                _target.AddPlugin(_pluginToAdd);
                //reset the choice
                _pluginToAdd = null;
                //reInit the plugins
                InitPlugins();
            }
            EditorGUI.EndDisabledGroup();

            if (position == Rect.zero)
            {
                GUILayout.EndVertical();
            }
        }