Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Passing information from editor window to script

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Nov 4, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am trying to send information from an editor window to all instances of a script.
    What I am trying to achieve in total is to have a boolean that effects the visibility of a Gizmo and have a colorfield change the color of all those gizmos.


    This is the editor window, the issue is the line
    Code (CSharp):
    1. FloorTiles.OnGizmoActive(navColor);
    .
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class EditorManager : EditorWindow {
    6.  
    7.     bool showNav;
    8.     public Color navColor = Color.green;
    9.  
    10.     bool ToolsFoldout = true;
    11.  
    12.     Color headerColor = new Color(0.65f, 0.65f, 0.65f, 1);
    13.     //Color backgroundColor = new Color(0.75f, 0.75f, 0.75f);
    14.  
    15.     [MenuItem("Tools/Labyrith/Manager")]
    16.     public static void Init()
    17.     {
    18.         EditorWindow.GetWindow(typeof(EditorManager), false, "Manager");
    19.     }
    20.  
    21.     void OnGUI()
    22.     {
    23.         GUI.skin.label.wordWrap = true;
    24.  
    25.         this.minSize = new Vector2(250, 350);
    26.  
    27.         if (!EditorGUIUtility.isProSkin)
    28.         {
    29.             headerColor = new Color(165 / 255f, 165 / 255f, 165 / 255f, 1);
    30.             //backgroundColor = new Color(193 / 255f, 193 / 255f, 193 / 255f, 1);
    31.         }
    32.         else
    33.         {
    34.             headerColor = new Color(41 / 255f, 41 / 255f, 41 / 255f, 1);
    35.             //backgroundColor = new Color(56 / 255f, 56 / 255f, 56 / 255f, 1);
    36.         }
    37.         GUILayout.Label("Manager", EditorStyles.boldLabel);
    38.  
    39.         ToolsFoldout = DrawHeaderTitle("Tools", ToolsFoldout, headerColor);
    40.         //ToolsFoldout = EditorGUILayout.Foldout(ToolsFoldout, "Tools");
    41.         if (ToolsFoldout)
    42.         {
    43.             //public ObjectType objectType;
    44.             GUILayout.Label("NavSystem Visibility", EditorStyles.boldLabel);
    45.             showNav = EditorGUILayout.Toggle("Show NavSystem", showNav);
    46.             navColor = EditorGUILayout.ColorField("NavSystem Colour", navColor);
    47.  
    48.             if(showNav == true)
    49.             {
    50.                 FloorTiles.OnGizmoActive(navColor);
    51.             }
    52.         }
    53.     }
    54.  
    55.     public bool DrawHeaderTitle(string title, bool foldoutProperty, Color backgroundColor)
    56.     {
    57.  
    58.         GUILayout.Space(0);
    59.  
    60.         GUI.Box(new Rect(1, GUILayoutUtility.GetLastRect().y + 4, position.width, 27), "");
    61.         EditorGUI.DrawRect(new Rect(GUILayoutUtility.GetLastRect().x, GUILayoutUtility.GetLastRect().y + 5f, position.width + 1, 25f), headerColor);
    62.         GUILayout.Space(4);
    63.  
    64.         GUILayout.Label(title, EditorStyles.largeLabel);
    65.         GUI.color = Color.clear;
    66.         if (GUI.Button(new Rect(0, GUILayoutUtility.GetLastRect().y - 4, position.width, 27), ""))
    67.         {
    68.             foldoutProperty = !foldoutProperty;
    69.         }
    70.         GUI.color = Color.white;
    71.         return foldoutProperty;
    72.     }
    73. }
    And the FloorTiles script is here:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FloorTiles : MonoBehaviour {
    5.  
    6.     public void OnGizmoActive(Color gizmoColor)
    7.     {
    8.         Gizmos.color = gizmoColor;
    9.         Gizmos.DrawWireSphere(transform.position, 3f);
    10.     }
    11. }
     
  2. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    you must draw gizmos in one of these two methods:
    OnDrawGizmos()
    OnDrawGizmosSelected()

    so you just want to save the color. you can show / hide them in the "Gizmo" submenu of the game view.
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    For the moment, I am not even trying to draw them, but be able to set the color from the editor window script.
    This is the bit I am having trouble with.

    I am getting:
    An object reference is required to access non-static member `FloorTiles.OnDrawGizmos(UnityEngine.Color)
    When I try to affect all instances of FloorTiles.
     
  4. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421