private static void CreateChildren(SpriterDotNetBehaviour sdnBehaviour, Spriter spriter, GameObject parent) { SpriterEntity entity = spriter.Entities[0]; int maxObjects = 0; foreach (SpriterAnimation animation in entity.Animations) { foreach (SpriterMainLineKey mainKey in animation.MainlineKeys) { maxObjects = Math.Max(maxObjects, mainKey.ObjectRefs.Length); } } sdnBehaviour.Children = new GameObject[maxObjects]; sdnBehaviour.Pivots = new GameObject[maxObjects]; for (int i = 0; i < maxObjects; ++i) { GameObject pivot = new GameObject(); GameObject child = new GameObject(); sdnBehaviour.Pivots[i] = pivot; sdnBehaviour.Children[i] = child; pivot.transform.SetParent(parent.transform); child.transform.SetParent(pivot.transform); child.transform.localPosition = new Vector3(0, 0, DeltaZ * i); pivot.name = "pivot " + i; child.name = "child " + i; child.AddComponent<SpriteRenderer>(); } }
public override void OnInspectorGUI() { DrawDefaultInspector(); SpriterDotNetBehaviour sdnb = target as SpriterDotNetBehaviour; string[] layers = GetSortingLayerNames(); int currentIndex = -1; for (int i = 0; i < layers.Length; ++i) { if (layers[i] == sdnb.SortingLayer) { currentIndex = i; break; } } if (currentIndex < 0) { currentIndex = 0; } int choiceIndex = EditorGUILayout.Popup("Sorting Layer", currentIndex, layers); sdnb.SortingLayer = layers[choiceIndex]; sdnb.SortingOrder = EditorGUILayout.IntField("Sorting Order", sdnb.SortingOrder); EditorUtility.SetDirty(target); }
/// Enumerate available values void EnumerateValues() { data = (SpriterDotNetBehaviour) target; var animator = data.EditorData(); options = animator.GetAnimations().ToList().ToArray(); var current = animator.CurrentAnimation.Name; index = Array.IndexOf(options, current); N.Console.Log("Current index is: {0} ie. {1} of {2}", current, index, options.Length); }
/// Enumerate available values void EnumerateValues() { data = (SpriterDotNetBehaviour)target; var animator = data.EditorData(); options = animator.GetAnimations().ToList().ToArray(); var current = animator.CurrentAnimation.Name; index = Array.IndexOf(options, current); N.Console.Log("Current index is: {0} ie. {1} of {2}", current, index, options.Length); }
private static void CreateSpriter(string path) { string data = File.ReadAllText(path); Spriter spriter = SpriterReader.Default.Read(data); string rootFolder = Path.GetDirectoryName(path); string name = Path.GetFileNameWithoutExtension(path); SpriterData spriterData = CreateSpriterData(spriter, rootFolder, name); foreach (SpriterEntity entity in spriter.Entities) { GameObject go = new GameObject(entity.Name); GameObject sprites = new GameObject(ObjectNameSprites); GameObject metadata = new GameObject(ObjectNameMetadata); SpriterDotNetBehaviour behaviour = go.AddComponent <SpriterDotNetBehaviour>(); behaviour.UseNativeTags = UseNativeTags; if (HasSound(entity)) { go.AddComponent <AudioSource>(); } sprites.SetParent(go); metadata.SetParent(go); ChildData cd = new ChildData(); CreateSprites(entity, cd, spriter, sprites); CreateCollisionRectangles(entity, cd, spriter, metadata); CreatePoints(entity, cd, spriter, metadata); behaviour.EntityIndex = entity.Id; behaviour.enabled = true; behaviour.SpriterData = spriterData; behaviour.ChildData = cd; GameObject prefab = CreatePrefab(go, rootFolder); EntityImported(entity, prefab); } if (UseNativeTags) { CreateTags(spriter); } }
public override void OnInspectorGUI() { DrawDefaultInspector(); SpriterDotNetBehaviour sdnb = target as SpriterDotNetBehaviour; string[] layers = SortingLayer.layers.Select(l => l.name).ToArray(); int currentIndex = Array.IndexOf(layers, sdnb.SortingLayer); if (currentIndex < 0) { currentIndex = 0; } int choiceIndex = EditorGUILayout.Popup("Sorting Layer", currentIndex, layers); sdnb.SortingLayer = layers[choiceIndex]; sdnb.SortingOrder = EditorGUILayout.IntField("Sorting Order", sdnb.SortingOrder); }
public override void OnInspectorGUI() { DrawDefaultInspector(); SpriterDotNetBehaviour sdnb = target as SpriterDotNetBehaviour; string[] layers = GetSortingLayerNames(); int currentIndex = Array.IndexOf(layers, sdnb.SortingLayer); if (currentIndex < 0) { currentIndex = 0; } int choiceIndex = EditorGUILayout.Popup("Sorting Layer", currentIndex, layers); sdnb.SortingLayer = layers[choiceIndex]; EditorUtility.SetDirty(target); }
private static GameObject ReplacePrefab(GameObject go, GameObject prefab, string path) { GameObject existing = GameObject.Instantiate(prefab); MoveChild(go, existing, ObjectNameSprites); MoveChild(go, existing, ObjectNameMetadata); SpriterDotNetBehaviour sdnbNew = go.GetComponent <SpriterDotNetBehaviour>(); SpriterDotNetBehaviour sdnbExisting = existing.GetComponent <SpriterDotNetBehaviour>(); sdnbExisting.ChildData = sdnbNew.ChildData; sdnbExisting.EntityIndex = sdnbNew.EntityIndex; sdnbExisting.SpriterData = sdnbNew.SpriterData; sdnbExisting.UseNativeTags = sdnbNew.UseNativeTags; GameObject createdPrefab = PrefabUtility.ReplacePrefab(existing, prefab, ReplacePrefabOptions.Default); GameObject.DestroyImmediate(existing); return(createdPrefab); }
private static IEnumerator GetAudioClip(SdnFileEntry entry, string path, SpriterDotNetBehaviour spriterDotNetBehaviour, bool andAssign) { using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, AudioType.WAV)) { yield return(www.SendWebRequest()); if (www.result == UnityWebRequest.Result.ConnectionError) { Debug.Log(www.error); } else { entry.Sound = DownloadHandlerAudioClip.GetContent(www); if (andAssign) { spriterDotNetBehaviour.Animator.SoundProvider.Set(entry.FolderId, entry.FileId, entry.Sound); } } } }
public static SpriterDotNetBehaviour CreateSpriter(string path, string entityName, Transform parent = null) { GameObject go = new GameObject(entityName); SpriterDotNetBehaviour behaviour = go.AddComponent <SpriterDotNetBehaviour>(); SpriterEntity entity = FetchOrCacheSpriterEntityDataFromFile(path, entityName, behaviour); if (entity == null) { UnityEngine.Object.Destroy(go); return(null); } GameObject sprites = new GameObject(ObjectNameSprites); GameObject metadata = new GameObject(ObjectNameMetadata); behaviour.UseNativeTags = false; if (SpriterImporterUtil.HasSound(entity)) { go.AddComponent <AudioSource>(); } sprites.SetParent(go); metadata.SetParent(go); ChildData cd = new ChildData(); SpriterImporterUtil.CreateSprites(entity, cd, sprites); SpriterImporterUtil.CreateCollisionRectangles(entity, cd, metadata); SpriterImporterUtil.CreatePoints(entity, cd, metadata); cd.Verify(); behaviour.EntityIndex = entity.Id; behaviour.enabled = true; behaviour.ChildData = cd; go.transform.parent = parent; return(behaviour); }
public override void OnInspectorGUI() { DrawDefaultInspector(); SpriterDotNetBehaviour sdnb = target as SpriterDotNetBehaviour; string[] layers = SortingLayer.layers.Select(l => l.name).ToArray(); int currentIndex = Array.IndexOf(layers, sdnb.SortingLayer); if (currentIndex < 0) { currentIndex = 0; } int choiceIndex = EditorGUILayout.Popup("Sorting Layer", currentIndex, layers); sdnb.SortingLayer = layers[choiceIndex]; sdnb.SortingOrder = EditorGUILayout.IntField("Sorting Order", sdnb.SortingOrder); if (GUI.changed) { EditorUtility.SetDirty(sdnb); EditorSceneManager.MarkSceneDirty(sdnb.gameObject.scene); } }
private static void LoadSprites(SpriterDotNetBehaviour sdnBehaviour, Spriter spriter, string rootFolder) { sdnBehaviour.Folders = new SdnFolder[spriter.Folders.Length]; for (int i = 0; i < spriter.Folders.Length; ++i) { SpriterFolder folder = spriter.Folders[i]; SdnFolder sdnFolder = new SdnFolder(); sdnFolder.Files = new Sprite[folder.Files.Length]; sdnBehaviour.Folders[i] = sdnFolder; for (int j = 0; j < folder.Files.Length; ++j) { SpriterFile file = folder.Files[j]; string spritePath = rootFolder; spritePath += "/"; spritePath += file.Name; Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath); if (sprite == null) { Debug.LogWarning("Unable to load sprite: " + spritePath); continue; } sdnFolder.Files[j] = sprite; } } }
private static SpriterEntity FetchOrCacheSpriterEntityDataFromFile(string path, string entityName, SpriterDotNetBehaviour spriterDotNetBehaviour) { if (SpriterEntityDatas.TryGetValue(new PathToEntity(path, entityName), out SpriterEntityData cachedEntityData)) { spriterDotNetBehaviour.SpriterData = cachedEntityData.data; return(cachedEntityData.entity); } string data = File.ReadAllText(path); Spriter spriter = SpriterReader.Default.Read(data); string rootFolder = Path.GetDirectoryName(path); SpriterEntity requestedEntity = null; foreach (SpriterEntity entity in spriter.Entities) { bool isRequestedEntity = entity.Name == entityName; SpriterData spriterData = CreateSpriterData(spriter, rootFolder, spriterDotNetBehaviour, isRequestedEntity); SpriterEntityData entityData = new SpriterEntityData(entity, spriterData); SpriterEntityDatas[new PathToEntity(path, entity.Name)] = entityData; if (isRequestedEntity) { requestedEntity = entity; spriterDotNetBehaviour.SpriterData = spriterData; } } return(requestedEntity); }
private static IEnumerable <SdnFileEntry> LoadAssets(Spriter spriter, string rootFolder, SpriterDotNetBehaviour spriterDotNetBehaviour, bool andAssignAudioClips) { for (int i = 0; i < spriter.Folders.Length; ++i) { SpriterFolder folder = spriter.Folders[i]; for (int j = 0; j < folder.Files.Length; ++j) { SpriterFile file = folder.Files[j]; string path = rootFolder; path += "/"; path += file.Name; SdnFileEntry entry = new SdnFileEntry { FolderId = folder.Id, FileId = file.Id }; if (file.Type == SpriterFileType.Sound) { spriterDotNetBehaviour.StartCoroutine(GetAudioClip(entry, path, spriterDotNetBehaviour, andAssignAudioClips)); } else { entry.Sprite = LoadNewSprite(path); } yield return(entry); } } }
private static SpriterData CreateSpriterData(Spriter spriter, string rootFolder, SpriterDotNetBehaviour spriterDotNetBehaviour, bool andAssignAudioClips) { SpriterData data = ScriptableObject.CreateInstance <SpriterData>(); data.Spriter = spriter; data.FileEntries = LoadAssets(spriter, rootFolder, spriterDotNetBehaviour, andAssignAudioClips).ToArray(); return(data); }