Search Unity

Question How to extract the CustomEditor of a shader?

Discussion in 'General Graphics' started by rod_lopez, May 23, 2023.

  1. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    21
    Hi here,
    I can see the custom editor of a shader listed in the IDE debugger (under UnityEngine.Shader::string customEditor), but I can't seem to be able to retrieve the value (through reflection) nor am I able to find C# code that makes reference to it.

    Is there a way to grab it? (particularly in player mode, assuming that info doesn't get stripped)
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    I know it's not a good way to answer a question but I can't help myself but to ask why you'd need this?

    Even if you were able to get that information, custom shader editors are in editor-only code, so you wouldn't have access to them in a standalone build.
     
  3. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    21
    That they are Editor-only wouldn't mean you can't access the string at non-editor time :)
     
  4. aras-p

    aras-p

    Joined:
    Feb 17, 2022
    Posts:
    75
    There is an internal string customEditor on a Shader C# class (link), seemingly since Unity 2018. It is internal, so you'd need to use reflection to get it. When you say you can't get the value, how and what does not work? It is a property, so you'd want to get the getter function via reflection.
     
    rod_lopez likes this.
  5. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    21
    Hey Aras! Long time no see! :D
    This is what I am trying...
    Code (CSharp):
    1. var field  = typeof(UnityEngine.Shader).GetField("customEditor", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
    2. Debug.Assert(field != null && field.Name.Length > 0);
     
  6. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    21
    Oh... kill me now...

    I was using GetField, rather than GetProperty!! All fixed now

    Btw @aras-p (now I have you on the line :))
    It'd be nice to be able to follow the pattern set by SetKeywordAndPass (from the LitGUI.cs) from non-editor-code (my scenario is a material created from meta-data on run-time, so I set the properties in the shader -which is fine- and also set the right keywords -which is what's set from you SetKeywordsAndPass-)

    This looking for the "customEditor" property is just a very convoluted way to help my guys write custom shader inspectors that can call a SetKeywordAndPass-like function (in non-editor time) in a way that's easy to remember.
     
    Last edited: May 23, 2023
  7. aras-p

    aras-p

    Joined:
    Feb 17, 2022
    Posts:
    75
    Yeah, you'd have to ask Unity people to make that happen :)