Advertisement
KaiClavier

SuperTextAdapter.cs quickfix

Nov 15th, 2019
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. using System.Reflection;
  7.  
  8. namespace Fungus
  9. {
  10.     public class SuperTextAdapter : IWriterTextDestination
  11.     {
  12.         protected Text textUI;
  13.         protected InputField inputField;
  14.         protected TextMesh textMesh;
  15.         protected SuperTextMesh stm;
  16.  
  17.         protected Component textComponent;
  18.         protected PropertyInfo textProperty;
  19.         protected IWriterTextDestination writerTextDestination;
  20.  
  21.         public void InitFromGameObject(GameObject go, bool includeChildren = false)
  22.         {
  23.             if (go == null)
  24.             {
  25.                 return;
  26.             }
  27.  
  28.             if (!includeChildren)
  29.             {
  30.                 textUI = go.GetComponent<Text>();
  31.                 inputField = go.GetComponent<InputField>();
  32.                 textMesh = go.GetComponent<TextMesh>();
  33.                 stm = go.GetComponent<SuperTextMesh>();
  34.                 writerTextDestination = go.GetComponent<IWriterTextDestination>();
  35.             }
  36.             else
  37.             {
  38.                 textUI = go.GetComponentInChildren<Text>();
  39.                 inputField = go.GetComponentInChildren<InputField>();
  40.                 textMesh = go.GetComponentInChildren<TextMesh>();
  41.                 stm = go.GetComponentInChildren<SuperTextMesh>();
  42.                 writerTextDestination = go.GetComponentInChildren<IWriterTextDestination>();
  43.             }
  44.            
  45.             // Try to find any component with a text property
  46.             if (textUI == null && inputField == null && textMesh == null && writerTextDestination == null)
  47.             {
  48.                 Component[] allcomponents = null;
  49.                 if (!includeChildren)
  50.                     allcomponents = go.GetComponents<Component>();
  51.                 else
  52.                     allcomponents = go.GetComponentsInChildren<Component>();
  53.  
  54.                 for (int i = 0; i < allcomponents.Length; i++)
  55.                 {
  56.                     var c = allcomponents[i];
  57.                     textProperty = c.GetType().GetProperty("text");
  58.                     if (textProperty != null)
  59.                     {
  60.                         textComponent = c;
  61.                         break;
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.  
  67.         public void ForceRichText()
  68.         {
  69.             if (textUI != null)
  70.             {
  71.                 textUI.supportRichText = true;
  72.             }
  73.  
  74.             // Input Field does not support rich text
  75.  
  76.             if (textMesh != null)
  77.             {
  78.                 textMesh.richText = true;
  79.             }
  80.  
  81.             if(stm != null)
  82.             {
  83.                 stm.richText = true;
  84.             }
  85.  
  86.             if (writerTextDestination != null)
  87.             {
  88.                 writerTextDestination.ForceRichText();
  89.             }
  90.         }
  91.  
  92.         public void SetTextColor(Color textColor)
  93.         {
  94.             if (textUI != null)
  95.             {
  96.                 textUI.color = textColor;
  97.             }
  98.             else if (inputField != null)
  99.             {
  100.                 if (inputField.textComponent != null)
  101.                 {
  102.                     inputField.textComponent.color = textColor;
  103.                 }
  104.             }
  105.             else if (textMesh != null)
  106.             {
  107.                 textMesh.color = textColor;
  108.             }
  109.             else if (stm != null)
  110.             {
  111.                 stm.color = textColor;
  112.             }
  113.             else if (writerTextDestination != null)
  114.             {
  115.                 writerTextDestination.SetTextColor(textColor);
  116.             }
  117.         }
  118.  
  119.         public void SetTextAlpha(float textAlpha)
  120.         {
  121.             if (textUI != null)
  122.             {
  123.                 Color tempColor = textUI.color;
  124.                 tempColor.a = textAlpha;
  125.                 textUI.color = tempColor;
  126.             }
  127.             else if (inputField != null)
  128.             {
  129.                 if (inputField.textComponent != null)
  130.                 {
  131.                     Color tempColor = inputField.textComponent.color;
  132.                     tempColor.a = textAlpha;
  133.                     inputField.textComponent.color = tempColor;
  134.                 }
  135.             }
  136.             else if (textMesh != null)
  137.             {
  138.                 Color tempColor = textMesh.color;
  139.                 tempColor.a = textAlpha;
  140.                 textMesh.color = tempColor;
  141.             }
  142.             else if (stm != null)
  143.             {
  144.                 Color tempColor = stm.color;
  145.                 tempColor.a = textAlpha;
  146.                 stm.color = tempColor;
  147.             }
  148.             else if (writerTextDestination != null)
  149.             {
  150.                 writerTextDestination.SetTextAlpha(textAlpha);
  151.             }
  152.         }
  153.  
  154.         public bool HasTextObject()
  155.         {
  156.             return (textUI != null || inputField != null || textMesh != null || textComponent != null ||
  157.                 stm !=null ||
  158.                  writerTextDestination != null);
  159.         }
  160.  
  161.         public bool SupportsRichText()
  162.         {
  163.             if (textUI != null)
  164.             {
  165.                 return textUI.supportRichText;
  166.             }
  167.             if (inputField != null)
  168.             {
  169.                 return false;
  170.             }
  171.             if (textMesh != null)
  172.             {
  173.                 return textMesh.richText;
  174.             }
  175.             if (stm != null)
  176.             {
  177.                 return true;
  178.             }
  179.             if (writerTextDestination != null)
  180.             {
  181.                 return writerTextDestination.SupportsRichText();
  182.             }
  183.             return false;
  184.         }
  185.  
  186.         public virtual string Text
  187.         {
  188.             get
  189.             {
  190.                 if (textUI != null)
  191.                 {
  192.                     return textUI.text;
  193.                 }
  194.                 else if (inputField != null)
  195.                 {
  196.                     return inputField.text;
  197.                 }
  198.                 else if (writerTextDestination != null)
  199.                 {
  200.                     return Text;
  201.                 }
  202.                 else if (textMesh != null)
  203.                 {
  204.                     return textMesh.text;
  205.                 }
  206.                 else if (stm != null)
  207.                 {
  208.                     return stm.text;
  209.                 }
  210.                 else if (textProperty != null)
  211.                 {
  212.                     return textProperty.GetValue(textComponent, null) as string;
  213.                 }
  214.  
  215.                 return "";
  216.             }
  217.  
  218.             set
  219.             {
  220.                 if (textUI != null)
  221.                 {
  222.                     textUI.text = value;
  223.                 }
  224.                 else if (inputField != null)
  225.                 {
  226.                     inputField.text = value;
  227.                 }
  228.                 else if (writerTextDestination != null)
  229.                 {
  230.                     Text = value;
  231.                 }
  232.                 else if (textMesh != null)
  233.                 {
  234.                     textMesh.text = value;
  235.                 }
  236.                 else if (stm != null)
  237.                 {
  238.                     stm.text = value;
  239.                 }
  240.                 else if (textProperty != null)
  241.                 {
  242.                     textProperty.SetValue(textComponent, value, null);
  243.                 }
  244.             }
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement