Advertisement
Guest User

Untitled

a guest
Feb 5th, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7.  
  8. namespace UI.Xml
  9. {
  10.     [ExecuteInEditMode]
  11.     class XmlLayout_Example_ColorSchemeManager : XmlLayoutController
  12.     {
  13.         public GameObject Root = null;
  14.  
  15.         private string colorScheme
  16.         {
  17.             get { return PlayerPrefs.GetString("colorScheme"); }
  18.             set { PlayerPrefs.SetString("colorScheme", value); }
  19.         }
  20.  
  21.         private void OnEnable()
  22.         {
  23.             if (string.IsNullOrEmpty(colorScheme)) colorScheme = "GreenYellow";
  24.  
  25.             ChangeColorSchemeWithoutRebuild(colorScheme);
  26.         }
  27.  
  28.         private void Start()
  29.         {
  30.             XmlLayoutTimer.AtEndOfFrame(() =>
  31.             {
  32.                 xmlLayout.GetElementById<Dropdown>("colorSchemeDropdown").SetSelectedValue(colorScheme);
  33.  
  34.                 ChangeColorScheme(colorScheme);
  35.             }, this, true);
  36.         }
  37.  
  38.         public void ChangeColorSchemeWithoutRebuild(string newScheme)
  39.         {
  40.             colorScheme = newScheme;
  41.  
  42.             var colorSchemeFile = XmlLayoutUtilities.LoadResource<TextAsset>(string.Format("Xml/ColorSchemes/{0}", newScheme));
  43.  
  44.             if (colorSchemeFile == null)
  45.             {
  46.                 Debug.LogErrorFormat("[XmlLayout][Example][Color Scheme Manager] Warning: unable to locate color scheme definition '{0}'", newScheme);
  47.                 return;
  48.             }
  49.  
  50.             List<XmlLayout> xmlLayouts = Root.gameObject
  51.                                              .GetComponentsInChildren<XmlLayout>(true)
  52.                                              .ToList();
  53.  
  54.             foreach (var layout in xmlLayouts)
  55.             {
  56.                 // skip this layout
  57.                 if (layout == this.xmlLayout) continue;
  58.  
  59.                 if (layout.DefaultsFiles == null) layout.DefaultsFiles = new List<TextAsset>();
  60.  
  61.                 layout.DefaultsFiles.Clear();
  62.                 layout.DefaultsFiles.Add(colorSchemeFile);
  63.             }
  64.         }
  65.  
  66.         public void ChangeColorScheme(string newScheme)
  67.         {
  68.             colorScheme = newScheme;
  69.  
  70.             var colorSchemeFile = XmlLayoutUtilities.LoadResource<TextAsset>(string.Format("Xml/ColorSchemes/{0}", newScheme));
  71.  
  72.             if (colorSchemeFile == null)
  73.             {
  74.                 Debug.LogErrorFormat("[XmlLayout][Example][Color Scheme Manager] Warning: unable to locate color scheme definition '{0}'", newScheme);
  75.                 return;
  76.             }
  77.  
  78.             List<XmlLayout> xmlLayouts = Root.gameObject
  79.                                              .GetComponentsInChildren<XmlLayout>(true)
  80.                                              .ToList();
  81.  
  82.             foreach(var layout in xmlLayouts)
  83.             {
  84.                 // skip this layout
  85.                 if (layout == this.xmlLayout) continue;
  86.  
  87.                 if (layout.DefaultsFiles == null) layout.DefaultsFiles = new List<TextAsset>();
  88.  
  89.                 var inactive = !layout.gameObject.activeSelf;
  90.  
  91.                 if (inactive)
  92.                 {
  93.                     layout.gameObject.SetActive(true);
  94.                 }
  95.  
  96.                 layout.DefaultsFiles.Clear();
  97.                 layout.DefaultsFiles.Add(colorSchemeFile);
  98.  
  99.                 layout.RebuildLayout(true);
  100.  
  101.                 if (inactive)
  102.                 {
  103.                     // copy the local variable (if we use 'layout' it will reference the foreach variable which changes through each iteration)
  104.                     var layoutTemp = layout;
  105.  
  106.                     // hide the layout again at the end of the frame
  107.                     XmlLayoutTimer.AtEndOfFrame(() =>
  108.                     {
  109.                         if (layoutTemp == null) return;
  110.  
  111.                         //canvasGroup.alpha = alphaBefore;
  112.                         layoutTemp.gameObject.SetActive(false);
  113.                     }, layoutTemp, true);
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement