KaiClavier

STMOutlineGenerator.cs OLD

Jul 31st, 2020 (edited)
2,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /*
  5.  
  6. Make SURE that outlineParent is a gameObject that's is above STM in the hierarchy for UI elements.
  7. Script isn't perfect yet, sometimes gameobjects don't get deleted when they should, but it seems to work fine in Play mode.
  8.  
  9. */
  10.  
  11. [ExecuteInEditMode]
  12. public class STMOutlineGenerator : MonoBehaviour {
  13.  
  14.     public SuperTextMesh superTextMesh;
  15.     [Tooltip("Make sure this is ABOVE your STM in the hierarchy for UI objects!!")]
  16.     public Transform outlineParent;
  17.     [Range(0,16)]
  18.     public int detailLevel = 8;
  19.     public float size = 0.05f;
  20.     public Color32 color;
  21.     [Tooltip("Offset text with this")]
  22.     public Vector3 offset = new Vector3(0f,0f,0.01f);
  23.     public bool updateEveryFrame = true;
  24.     //public float distance = 0.001f;
  25.  
  26.     private Mesh sharedOutlineMesh = null;
  27.     private GameObject[] outlineObjects = new GameObject[0];
  28.  
  29.     [System.Serializable]
  30.     public class OutlineRenderer
  31.     {
  32.         public Transform transform;
  33.         public Vector3 offset;
  34.         public GameObject gameObject;
  35.         public MeshFilter meshFilter;
  36.         public MeshRenderer meshRenderer;
  37.         public CanvasRenderer canvasRenderer;
  38.     }
  39.     private OutlineRenderer[] allRenderers = new OutlineRenderer[0];
  40.  
  41.     private OutlineRenderer newRenderer;
  42.  
  43.     public void Reset()
  44.     {
  45.         superTextMesh = GetComponent<SuperTextMesh>();
  46.        
  47.     }
  48.     public void OnEnable()
  49.     {
  50.         if(Application.isPlaying)
  51.         {
  52.             superTextMesh.OnRebuildEvent += GenerateOutlines;
  53.         }
  54.     }
  55.     public void OnDisable()
  56.     {
  57.         if(Application.isPlaying)
  58.         {
  59.             superTextMesh.OnRebuildEvent -= GenerateOutlines;
  60.         }
  61.     }
  62.     private bool validate;
  63.     public void OnValidate()
  64.     {
  65.         validate = true;
  66.     }
  67.     public void Update()
  68.     {  
  69.         if(!Application.isPlaying && validate)
  70.         {
  71.             validate = false;
  72.             GenerateOutlines();
  73.         }
  74.     }
  75.     public void LateUpdate()
  76.     {
  77.         if(Application.isPlaying && updateEveryFrame)
  78.         {
  79.             RefreshOutlines();
  80.         }
  81.     }
  82.     public void GenerateOutlines(Vector3[] verts, Vector3[] middles, Vector3[] positions)
  83.     {
  84.         GenerateOutlines();
  85.     }
  86.     public void RefreshOutlines()
  87.     {  
  88.         for(int i=0; i<detailLevel; i++)
  89.         {
  90.             if(superTextMesh.uiMode)
  91.             {
  92.                 allRenderers[i].canvasRenderer.SetMesh(superTextMesh.textMesh);
  93.             }
  94.             else
  95.             {
  96.                 CloneTextMesh();
  97.                 allRenderers[i].meshFilter.sharedMesh = sharedOutlineMesh;
  98.             }
  99.         }
  100.     }
  101.     private void CloneTextMesh()
  102.     {
  103.         sharedOutlineMesh = new Mesh();
  104.         if(superTextMesh.textMesh == null)
  105.             return;
  106.        
  107.         sharedOutlineMesh.vertices = superTextMesh.textMesh.vertices;
  108.         sharedOutlineMesh.triangles = superTextMesh.textMesh.triangles;
  109.         sharedOutlineMesh.normals = superTextMesh.textMesh.normals;
  110.         sharedOutlineMesh.uv = superTextMesh.textMesh.uv;
  111.         sharedOutlineMesh.uv2 = superTextMesh.textMesh.uv2;
  112.         //colors
  113.         colors = superTextMesh.textMesh.colors32;
  114.         for(int j=0; j<colors.Length; j++)
  115.         {
  116.             colors[j] = color; //assign outline color
  117.         }
  118.         sharedOutlineMesh.colors32 = colors;
  119.     }
  120.  
  121.     private Color32[] colors;
  122.     public void GenerateOutlines()
  123.     {
  124.         if(superTextMesh != null && outlineParent != null)
  125.         {
  126.             sharedOutlineMesh = null; //clear last mesh
  127.  
  128.             for(int i=0; i<outlineObjects.Length; i++)
  129.             {
  130.                 if(Application.isPlaying)
  131.                 {
  132.                     Destroy(outlineObjects[i]);
  133.                 }
  134.                 else
  135.                 {
  136.                     DestroyImmediate(outlineObjects[i]);
  137.                 }
  138.             }
  139.             foreach(Transform child in outlineParent)
  140.             {
  141.                 //destroy all
  142.                 if(Application.isPlaying)
  143.                 {
  144.                     Destroy(child.gameObject);
  145.                 }
  146.                 else
  147.                 {
  148.                     DestroyImmediate(child.gameObject);
  149.                 }
  150.             }
  151.  
  152.             //recreate all
  153.             allRenderers = new OutlineRenderer[detailLevel];
  154.             outlineObjects = new GameObject[detailLevel];
  155.             for(int i=0; i<allRenderers.Length; i++)
  156.             {
  157.                 //create outline gameobjects
  158.                 newRenderer = new OutlineRenderer();
  159.                 newRenderer.gameObject = new GameObject();
  160.                 outlineObjects[i] = newRenderer.gameObject;
  161.                 newRenderer.transform = newRenderer.gameObject.transform;
  162.                 newRenderer.transform.name = "Outline";
  163.                 newRenderer.transform.SetParent(outlineParent); //parent to STM
  164.                
  165.  
  166.                 //UI text
  167.                 if(superTextMesh.uiMode && superTextMesh.c.GetMaterial() != null)
  168.                 {
  169.                     newRenderer.canvasRenderer = newRenderer.gameObject.AddComponent<CanvasRenderer>();
  170.                     newRenderer.canvasRenderer.SetMesh(superTextMesh.textMesh);
  171.                     if(superTextMesh.c.GetMaterial().HasProperty("_MainTex"))
  172.                     {
  173.                         newRenderer.canvasRenderer.SetTexture(superTextMesh.c.GetMaterial().GetTexture("_MainTex"));
  174.                        
  175.                         newRenderer.canvasRenderer.materialCount = 1;
  176.                         newRenderer.canvasRenderer.SetMaterial(superTextMesh.c.GetMaterial(), 0);
  177.                     }
  178.                     newRenderer.canvasRenderer.SetColor(color);
  179.                 }
  180.                 //regular text
  181.                 else if(superTextMesh.r.sharedMaterials != null)
  182.                 {
  183.                    
  184.                     newRenderer.meshFilter = newRenderer.gameObject.AddComponent<MeshFilter>();
  185.                     newRenderer.meshRenderer = newRenderer.gameObject.AddComponent<MeshRenderer>();
  186.                    
  187.  
  188.                     if(sharedOutlineMesh == null)
  189.                     {
  190.                         CloneTextMesh();
  191.                     }
  192.  
  193.                     newRenderer.meshFilter.sharedMesh = sharedOutlineMesh;
  194.                     newRenderer.meshRenderer.sharedMaterials = superTextMesh.r.sharedMaterials;
  195.  
  196.        
  197.                 }
  198.                 //give it an offset... for now uhh
  199.                 newRenderer.offset.x = (superTextMesh.t.position.x + Mathf.Cos(Mathf.PI * 2f * ((float)i/detailLevel)) * size) + offset.x;
  200.                 newRenderer.offset.y = (superTextMesh.t.position.y + Mathf.Sin(Mathf.PI * 2f * ((float)i/detailLevel)) * size) + offset.y;
  201.                 newRenderer.offset.z = (superTextMesh.t.position.z) + offset.z;
  202.  
  203.                 newRenderer.transform.position = newRenderer.offset;
  204.  
  205.                 //assign to array
  206.                 allRenderers[i] = newRenderer;
  207.             }
  208.         }
  209.     }
  210. }
  211.  
Add Comment
Please, Sign In to add comment