Example #1
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);
        }
 public static void CreateDynamicDNAConverterController()
 {
     DynamicDNAConverterController.CreateDynamicDNAConverterControllerAsset();
 }