static void ImportScml(string assetPath) { string folderPath = Path.GetDirectoryName(assetPath); var doc = new XmlDocument(); doc.Load(assetPath); //Parse the SCML file var scml = new Spriter.ScmlObject(doc); //TODO: Verify that all files/folders exist var pb = new PrefabBuilder(); foreach (var entity in scml.Entities) { var go = pb.MakePrefab(entity); var prefabPath = Path.Combine(folderPath, go.name + ".prefab"); //Change to forward slash for asset database friendliness prefabPath = prefabPath.Replace('\\', '/'); //Create prefab and destroy the object in scene PrefabUtility.CreatePrefab(prefabPath, go); //Add animations to prefab object var anim = new AnimationBuilder(); anim.BuildAnimationClips(go, entity, prefabPath); GameObject.DestroyImmediate(go); } }
static void ImportScml(string assetPath) { string folderPath = Path.GetDirectoryName(assetPath); //Load the SCML as XML var doc = new XmlDocument(); doc.Load(assetPath); //Parse the SCML file var scml = new Spriter.ScmlObject(doc); //TODO: Verify that all files/folders exist var pb = new PrefabBuilder(); foreach (var entity in scml.Entities) { //TODO: Settings file to customize prefab location var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab"); //Change to forward slash for asset database friendliness prefabPath = prefabPath.Replace('\\', '/'); //Either instantiate the existing prefab or create a new one GameObject go; var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)); if (prefabGo == null) { go = new GameObject(); prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab); } else { go = GameObject.Instantiate(prefabGo) as GameObject; var oldAnimator = go.GetComponent <Animator>(); if (oldAnimator) { GameObject.DestroyImmediate(oldAnimator); } } //Build the prefab based on the supplied entity pb.MakePrefab(entity, go, folderPath); var animator = go.AddComponent <Animator>(); //Add animations to prefab object var anim = new AnimationBuilder(); var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath); AssetDatabase.SaveAssets(); var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller"); var oldController = (AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof(AnimatorController)); var controller = oldController; if (!oldController) { controller = AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath); foreach (var animationClip in allAnimClips) { if (animationClip) { #if UNITY_5 controller.AddMotion(animationClip); #else AnimatorController.AddAnimationClipToController(controller, animationClip); #endif } } } AnimatorController.SetAnimatorController(animator, controller); go.SetActive(true); //Update the prefab PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab); //Add a generic avatar - because why not? //TODO: May need to eventually break this into a separate class // ie: if we want to look for a root motion node by naming convention //var avatar = AvatarBuilder.BuildGenericAvatar(go, ""); //avatar.name = go.name; //AssetDatabase.AddObjectToAsset(avatar, prefabPath); GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); } }
static void ImportScml(string assetPath) { string folderPath = Path.GetDirectoryName(assetPath); //Load the SCML as XML var doc = new XmlDocument(); doc.Load(assetPath); //Parse the SCML file var scml = new Spriter.ScmlObject(doc); //TODO: Verify that all files/folders exist var pb = new PrefabBuilder(); foreach (var entity in scml.Entities) { //TODO: Settings file to customize prefab location var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab"); //Change to forward slash for asset database friendliness prefabPath = prefabPath.Replace('\\', '/'); //Either instantiate the existing prefab or create a new one GameObject go; var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)); if (prefabGo == null) { go = new GameObject(); prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab); } else { go = GameObject.Instantiate(prefabGo) as GameObject; var oldAnimator = go.GetComponent<Animator>(); if (oldAnimator) GameObject.DestroyImmediate(oldAnimator); } //Build the prefab based on the supplied entity pb.MakePrefab(entity, go, folderPath); var animator = go.AddComponent<Animator>(); //Add animations to prefab object var anim = new AnimationBuilder(); var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath); AssetDatabase.SaveAssets(); var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller"); UnityEditor.Animations.AnimatorController oldController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof (UnityEditor.Animations.AnimatorController)); UnityEditor.Animations.AnimatorController controller = oldController; if (!oldController) { controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath); foreach (var animationClip in allAnimClips) { if (animationClip) { #if UNITY_5 controller.AddMotion(animationClip); #else AnimatorController.AddAnimationClipToController(controller, animationClip); #endif } } } UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller); go.SetActive(true); //Update the prefab PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab); //Add a generic avatar - because why not? //TODO: May need to eventually break this into a separate class // ie: if we want to look for a root motion node by naming convention //var avatar = AvatarBuilder.BuildGenericAvatar(go, ""); //avatar.name = go.name; //AssetDatabase.AddObjectToAsset(avatar, prefabPath); GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); } }
static void ImportScml(string assetPath) { string folderPath = Path.GetDirectoryName(assetPath); var doc = new XmlDocument(); doc.Load(assetPath); //Parse the SCML file var scml = new Spriter.ScmlObject(doc); //TODO: Verify that all files/folders exist var pb = new PrefabBuilder(); foreach (var entity in scml.Entities) { //TODO: Settings file to customize prefab location var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab"); //Change to forward slash for asset database friendliness prefabPath = prefabPath.Replace('\\', '/'); //Either instantiate the existing prefab or create a new one GameObject go; var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)); if (prefabGo == null) { go = new GameObject(); prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab); } else { go = GameObject.Instantiate(prefabGo) as GameObject; //Destroy CharacterMap (if it exists) var charmap = go.GetComponent<CharacterMap>(); if (charmap) GameObject.DestroyImmediate(charmap); } //Build the prefab based on the supplied entity pb.MakePrefab(entity, go, folderPath); //Update the prefab PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab); //Add animations to prefab object var anim = new AnimationBuilder(); anim.BuildAnimationClips(go, entity, prefabPath); //Add a generic avatar - because why not? //TODO: May need to eventually break this into a separate class // ie: if we want to look for a root motion node by naming convention //var avatar = AvatarBuilder.BuildGenericAvatar(go, ""); //avatar.name = go.name; //AssetDatabase.AddObjectToAsset(avatar, prefabPath); GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); } }