using UnityEngine; using System.Collections.Generic; using System; using MalbersAnimations.Events; namespace MalbersAnimations.Utilities { [AddComponentMenu("Malbers/Utilities/Mesh/Active Meshes")] public class ActiveMeshes : MonoBehaviour { public List Meshes = new List(); public bool showMeshesList = true; public ActiveSMesh this[int index] { get => Meshes[index]; set => Meshes[index] = value; } public int Count => Meshes.Count; /// All Active Meshes Index stored on a string separated by a space ' ' public string AllIndex { set { string[] getIndex = value.Split(' '); for (int i = 0; i < Count; i++) { if (getIndex.Length > i) { if (int.TryParse(getIndex[i], out int index)) { if (index == -1) continue; Meshes[i].ChangeMesh(index); } } } } get { string AllIndex = ""; for (int i = 0; i < Count; i++) { AllIndex += Meshes[i].Current.ToString() + " "; } AllIndex.Remove(AllIndex.Length - 1); //Remove the last space } return AllIndex; } } public bool random; private void Awake() { if (random) { Randomize(); } } /// Randomize all the Active Meshes on the list public void Randomize() { foreach (var mat in Meshes) { mat.ChangeMesh(UnityEngine.Random.Range(0, mat.meshes.Length)); } } /// Set All ActiveMeshes Index from the Meshes list. public void SetActiveMeshesIndex(int[] MeshesIndex) { if (MeshesIndex.Length != Count) { Debug.LogError("Meshes Index array Lenghts don't match"); return; } for (int i = 0; i < MeshesIndex.Length; i++) { Meshes[i].ChangeMesh(MeshesIndex[i]); } } /// Select an Element from the List by its index, and change to the next variation. public virtual void ChangeMesh(int index) => Meshes[index % Count].ChangeMesh(); /// Select an Element from the List by the index, and change to a variation. using its index public virtual void ChangeMesh(int indexList, int IndexMesh) => Meshes[indexList % Count].ChangeMesh(IndexMesh - 1); /// Change to next mesh using the name public virtual void ChangeMesh(string name, bool next) { ActiveSMesh mesh = Meshes.Find(item => item.Name == name); mesh?.ChangeMesh(next); } public virtual void ChangeMesh(string name) => ChangeMesh(name, true); public virtual void ChangeMesh(string name, int CurrentIndex) { ActiveSMesh mesh = Meshes.Find(item => item.Name == name); mesh?.ChangeMesh(CurrentIndex); } public virtual void ChangeMesh(int index, bool next) => Meshes[index].ChangeMesh(next); /// Toggle all meshes on the list public virtual void ChangeMesh(bool next = true) { foreach (var mesh in Meshes) mesh.ChangeMesh(next); } /// Get the Active mesh by their name public virtual ActiveSMesh GetActiveMesh(string name) { if (Count == 0) return null; return Meshes.Find(item => item.Name == name); } /// Get the Active Mesh by their Index public virtual ActiveSMesh GetActiveMesh(int index) { if (Count == 0) return null; if (index >= Count) index = 0; if (index < 0) index = Count - 1; return Meshes[index]; } #if UNITY_EDITOR [ContextMenu("Create Event Listeners")] void CreateListeners() { MEventListener listener = this.FindComponent(); if (listener == null) listener = transform.root.gameObject.AddComponent(); if (listener.Events == null) listener.Events = new List(); MEvent BlendS = MTools.GetInstance("Change Mesh"); if (listener.Events.Find(item => item.Event == BlendS) == null) { var item = new MEventItemListener() { Event = BlendS, useVoid = false, useString = true, useInt = true }; UnityEditor.Events.UnityEventTools.AddPersistentListener(item.ResponseInt, ChangeMesh); UnityEditor.Events.UnityEventTools.AddPersistentListener(item.ResponseString, ChangeMesh); listener.Events.Add(item); Debug.Log("Change Mesh Added to the Event Listeners"); UnityEditor.EditorUtility.SetDirty(listener); MTools.SetDirty(listener); } } #endif } [Serializable] public class ActiveSMesh { [HideInInspector] public string Name = "NameHere"; public Transform[] meshes; [HideInInspector,SerializeField] public int Current; [Space,Header("Invoked when the Active mesh is changed")] public TransformEvent OnActiveMesh = new TransformEvent(); /// Change mesh to the Next/Before public virtual void ChangeMesh(bool next = true) { if (next) Current++; else Current--; if (Current >= meshes.Length) Current = 0; if (Current < 0) Current = meshes.Length - 1; foreach (var item in meshes) { if (item) item.gameObject.SetActive(false); } if (meshes[Current]) { meshes[Current].gameObject.SetActive(true); OnActiveMesh.Invoke(meshes[Current]); } } /// Returns the Active Transform public virtual Transform GetCurrentActiveMesh() { return meshes[Current]; } /// Set a mesh by Index public virtual void ChangeMesh(int Index) { Current = Index-1; ChangeMesh(); } public void Set_by_BinaryIndex(int binaryCurrent) { int current = 0; for (int i = 0; i < meshes.Length; i++) { if (MTools.IsBitActive(binaryCurrent, i)) { current = i; //Find the first active bit and use it as current break; } } ChangeMesh(current); } } }