Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         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();
                     if (oldAnimator) GameObject.DestroyImmediate(oldAnimator);
                 }

                 //Build the prefab based on the supplied entity
                 pb.MakePrefab(entity, go, folderPath);

                 var animator = go.AddComponent();



                 //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)
                         {
                             //UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
                         }
                     }
                 }
                 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();
             }
         }