static void InstanceCanvasActor() { foreach (object obj in Selection.objects) { ActorAsset actorAsset = obj as ActorAsset; string actorInstanceName = actorAsset.name; GameObject go = new GameObject(actorInstanceName, typeof(RectTransform), typeof(ActorCanvasComponent)); go.GetComponent <ActorCanvasComponent>().SetActorAsset(actorAsset); } }
static void InstanceSingleMeshActor() { foreach (object obj in Selection.objects) { ActorAsset actorAsset = obj as ActorAsset; string actorInstanceName = actorAsset.name; GameObject go = new GameObject(actorInstanceName, typeof(MeshFilter), typeof(MeshRenderer), typeof(ActorSingleMeshComponent)); go.GetComponent <ActorSingleMeshComponent>().SetActorAsset(actorAsset); } }
private static void UpdateGameObjectsFor(ActorAsset actorAsset) { Object[] list = Resources.FindObjectsOfTypeAll(typeof(GameObject)); foreach (Object obj in list) { GameObject go = obj as GameObject; ActorComponent actor = go.GetComponent <ActorComponent>(); if (actor != null && actor.Asset == actorAsset) { Debug.Log("FOUND ACTOR WITH UPDATED ASSET"); // We found an actor using the asset that got updated. Let's update the game object. actor.InitializeFromAsset(actorAsset); } } }
static void MakeMecanimController() { foreach (object obj in Selection.objects) { ActorAsset actorAsset = obj as ActorAsset; string actorPath = AssetDatabase.GetAssetPath(actorAsset); actorPath = actorPath.Replace(".asset", ""); Debug.Log("Actor Path " + actorPath); int idx = 0; string filename = null; while (true) { filename = idx == 0 ? actorPath + ".controller" : actorPath + "_" + idx + ".controller"; if (!File.Exists(filename)) { break; } idx++; } UnityEditor.Animations.AnimatorController animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(filename) as UnityEditor.Animations.AnimatorController; if (!actorAsset.Loaded) { actorAsset.Load(); } Nima.Actor actor = actorAsset.Actor; foreach (Nima.Animation.ActorAnimation actorAnimation in actor.Animations) { AnimationClip animationClip = new AnimationClip(); animationClip.name = actorAnimation.Name; animationClip.wrapMode = actorAnimation.IsLooping ? WrapMode.Loop : WrapMode.ClampForever; AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(animationClip); clipSettings.stopTime = actorAnimation.Duration; clipSettings.loopTime = actorAnimation.IsLooping; AnimationUtility.SetAnimationClipSettings(animationClip, clipSettings); AssetDatabase.AddObjectToAsset(animationClip, animatorController); EditorUtility.SetDirty(animationClip); } } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); }
static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[] moved, string[] movedFromAssetPaths) { foreach (string path in imported) { string extension = Path.GetExtension(path).ToLower(); string filename = Path.GetFileNameWithoutExtension(path); string secondExtension = Path.GetExtension(filename); // This is our identifier for a Nima Character being added to the assets. // Due to how Unity implements custom assets, we have to use the .bytes extension to support it being accessed as a TextAsset type. if ( (secondExtension == ".nma" && extension == ".bytes") || (secondExtension == ".nima" && extension == ".bytes")) { // Get filename without .nima.bytes filename = Path.GetFileNameWithoutExtension(filename); TextAsset rawAsset = AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset)) as TextAsset; if (rawAsset != null) { string assetPath = Path.GetDirectoryName(path) + "/" + filename + ".asset"; // Load or make the asset that will be responsible for loading and sharing nodes, meshes, etc across all game object instances. ActorAsset actorAsset = (ActorAsset)AssetDatabase.LoadAssetAtPath(assetPath, typeof(ActorAsset)); if (actorAsset != null) { //Debug.Log("RELOADING ACTOR ASSET"); // If the load succeeds, update the GameObjects that are referencing this asset. if (actorAsset.Load(rawAsset)) { // Do we really want to do this? If the user had customized the character, the customizations would get blown away. // Leave it out for now. //UpdateGameObjectsFor(actorAsset); } } else { //Debug.Log("LOADING ACTOR ASSET"); actorAsset = ScriptableObject.CreateInstance <ActorAsset>(); if (actorAsset.Load(rawAsset)) { AssetDatabase.CreateAsset(actorAsset, assetPath); if (actorAsset.Load(rawAsset)) { AssetDatabase.SaveAssets(); } } } // Finally sync up the materials. if (actorAsset != null) { int neededMaterials = actorAsset.Actor.TexturesUsed; if (actorAsset.m_TextureMaterials == null || actorAsset.m_TextureMaterials.Length != neededMaterials) { actorAsset.m_TextureMaterials = new Material[neededMaterials]; } if (neededMaterials == 1) { Material m = GetMaterial(Path.GetDirectoryName(path) + "/" + filename); actorAsset.m_TextureMaterials[0] = m; } else { for (int i = 0; i < neededMaterials; i++) { Material m = GetMaterial(Path.GetDirectoryName(path) + "/" + filename + i); actorAsset.m_TextureMaterials[i] = m; } } // Force saving the asset. EditorUtility.SetDirty(actorAsset); } } /*Debug.Log("Got NIMA " + path); * using (FileStream fs = new FileStream(path, FileMode.Open)) * { * using (BinaryReader reader = new BinaryReader (fs)) * { * ActorAsset actorAsset = (ActorAsset)AssetDatabase.LoadAssetAtPath(path + ".asset", typeof(ActorAsset)); * // Maybe always overwrite here? * if(actorAsset != null) * { * Debug.Log("Updating existing actor."); * actorAsset.Load(reader); * } * else * { * Debug.Log("Loading new actor."); * actorAsset = ActorAsset.CreateInstance<ActorAsset>(); * if(actorAsset.Load(reader)) * { * AssetDatabase.CreateAsset(actorAsset, path + ".asset"); * actorAsset.m_Asset = * AssetDatabase.SaveAssets(); * } * } * } * }*/ } } }
public void InitializeFromAsset(ActorAsset actorAsset) { HideFlags hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild | HideFlags.DontUnloadUnusedAsset | HideFlags.HideInHierarchy | HideFlags.HideInInspector; m_ActorAsset = actorAsset; if (!actorAsset.Loaded && !actorAsset.Load()) { #if UNITY_EDITOR Debug.Log("Bad actorAsset referenced by Actor2D."); #endif return; } RemoveNodes(); m_ActorInstance = new Actor(); m_ActorInstance.Copy(m_ActorAsset.Actor); OnActorInstanced(); // Instance actor bones first as our image nodes need to know about them if they are skinned. { //ActorNode[] allNodes = m_ActorInstance.AllNodes; IList <Nima.ActorComponent> actorComponents = m_ActorInstance.Components; m_Nodes = new ActorNodeComponent[actorComponents.Count]; int imgNodeIdx = 0; for (int i = 0; i < actorComponents.Count; i++) { Nima.ActorComponent ac = actorComponents[i]; ActorNode an = ac as ActorNode; if (an == null) { continue; } GameObject go = null; ActorImage ai = an as ActorImage; if (ai != null) { ActorNodeComponent nodeComponent = MakeImageNodeComponent(imgNodeIdx, ai); nodeComponent.Node = ai; go = nodeComponent.gameObject; m_Nodes[i] = nodeComponent; imgNodeIdx++; } else { ActorCollider actorCollider = an as ActorCollider; if (actorCollider != null && InstanceColliders) { go = new GameObject(an.Name, typeof(ActorColliderComponent)); ActorColliderComponent colliderComponent = go.GetComponent <ActorColliderComponent>(); colliderComponent.Node = actorCollider; m_Nodes[i] = colliderComponent; } // else // { // go = new GameObject(an.Name, typeof(ActorNodeComponent)); // ActorNodeComponent nodeComponent = go.GetComponent<ActorNodeComponent>(); // nodeComponent.Node = an; // m_Nodes[i] = nodeComponent; // } } if (go != null) { go.hideFlags = hideFlags; } } // After they are all created, initialize them. for (int i = 0; i < m_Nodes.Length; i++) { ActorNodeComponent nodeComponent = m_Nodes[i]; if (nodeComponent != null) { nodeComponent.Initialize(this); } } } OnActorInitialized(); #if UNITY_EDITOR LateUpdate(); UpdateEditorBounds(); #endif }
public void SetActorAsset(ActorAsset asset) { m_ActorAsset = asset; InitializeFromAsset(m_ActorAsset); }