How can I change enum items using a button?











up vote
2
down vote

favorite












using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.IMGUI;

public class ChangeShaderRenderingModes : EditorWindow
{
public enum BlendMode
{
Opaque,
Cutout,
Fade,
Transparent
}

static Material material;
static Material objectMat;
static int modeIndex = 0;

[MenuItem("Tools/Change Rendering Modes")]
public static void ShowWindow()
{
GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
}

private void OnGUI()
{
if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
{
var objects = Selection.objects;

Shader shader = Shader.Find("Standard");
material = new Material(shader);

if (modeIndex == 4)
modeIndex = 0;

ChangeRenderMode(material, BlendMode.Opaque);

GameObject go = GameObject.Find("test");

objectMat = go.GetComponent<Renderer>().sharedMaterial;
objectMat = material;
go.GetComponent<Renderer>().sharedMaterial = material;
}
}

public static void ChangeRenderMode(Material standardShaderMaterial, BlendMode blendMode)
{
switch (blendMode)
{
case BlendMode.Opaque:
standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
standardShaderMaterial.SetInt("_ZWrite", 1);
standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
standardShaderMaterial.renderQueue = -1;
break;
case BlendMode.Cutout:
standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
standardShaderMaterial.SetInt("_ZWrite", 1);
standardShaderMaterial.EnableKeyword("_ALPHATEST_ON");
standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
standardShaderMaterial.renderQueue = 2450;
break;
case BlendMode.Fade:
standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
standardShaderMaterial.SetInt("_ZWrite", 0);
standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
standardShaderMaterial.EnableKeyword("_ALPHABLEND_ON");
standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
standardShaderMaterial.renderQueue = 3000;
break;
case BlendMode.Transparent:
standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
standardShaderMaterial.SetInt("_ZWrite", 0);
standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
standardShaderMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
standardShaderMaterial.renderQueue = 3000;
break;
}
}
}


I want to make that each time I click the button it will change to the next mode. Opaque, Cutout, Fade, Transparent



And after Transparent back to the first one Opaque



The problem is that the method ChangeRenderMode get one specific mode.
I want when clicking the button once ChangeRenderMode will get Opaque then next click it will get Cutout then Fade and Transparent.










share|improve this question


























    up vote
    2
    down vote

    favorite












    using System.Collections;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;
    using UnityEditor.IMGUI;

    public class ChangeShaderRenderingModes : EditorWindow
    {
    public enum BlendMode
    {
    Opaque,
    Cutout,
    Fade,
    Transparent
    }

    static Material material;
    static Material objectMat;
    static int modeIndex = 0;

    [MenuItem("Tools/Change Rendering Modes")]
    public static void ShowWindow()
    {
    GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
    }

    private void OnGUI()
    {
    if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
    {
    var objects = Selection.objects;

    Shader shader = Shader.Find("Standard");
    material = new Material(shader);

    if (modeIndex == 4)
    modeIndex = 0;

    ChangeRenderMode(material, BlendMode.Opaque);

    GameObject go = GameObject.Find("test");

    objectMat = go.GetComponent<Renderer>().sharedMaterial;
    objectMat = material;
    go.GetComponent<Renderer>().sharedMaterial = material;
    }
    }

    public static void ChangeRenderMode(Material standardShaderMaterial, BlendMode blendMode)
    {
    switch (blendMode)
    {
    case BlendMode.Opaque:
    standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    standardShaderMaterial.SetInt("_ZWrite", 1);
    standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
    standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
    standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    standardShaderMaterial.renderQueue = -1;
    break;
    case BlendMode.Cutout:
    standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    standardShaderMaterial.SetInt("_ZWrite", 1);
    standardShaderMaterial.EnableKeyword("_ALPHATEST_ON");
    standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
    standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    standardShaderMaterial.renderQueue = 2450;
    break;
    case BlendMode.Fade:
    standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    standardShaderMaterial.SetInt("_ZWrite", 0);
    standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
    standardShaderMaterial.EnableKeyword("_ALPHABLEND_ON");
    standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    standardShaderMaterial.renderQueue = 3000;
    break;
    case BlendMode.Transparent:
    standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    standardShaderMaterial.SetInt("_ZWrite", 0);
    standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
    standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
    standardShaderMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    standardShaderMaterial.renderQueue = 3000;
    break;
    }
    }
    }


    I want to make that each time I click the button it will change to the next mode. Opaque, Cutout, Fade, Transparent



    And after Transparent back to the first one Opaque



    The problem is that the method ChangeRenderMode get one specific mode.
    I want when clicking the button once ChangeRenderMode will get Opaque then next click it will get Cutout then Fade and Transparent.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      using System.Collections;
      using System.Collections.Generic;
      using UnityEditor;
      using UnityEngine;
      using UnityEditor.IMGUI;

      public class ChangeShaderRenderingModes : EditorWindow
      {
      public enum BlendMode
      {
      Opaque,
      Cutout,
      Fade,
      Transparent
      }

      static Material material;
      static Material objectMat;
      static int modeIndex = 0;

      [MenuItem("Tools/Change Rendering Modes")]
      public static void ShowWindow()
      {
      GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
      }

      private void OnGUI()
      {
      if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
      {
      var objects = Selection.objects;

      Shader shader = Shader.Find("Standard");
      material = new Material(shader);

      if (modeIndex == 4)
      modeIndex = 0;

      ChangeRenderMode(material, BlendMode.Opaque);

      GameObject go = GameObject.Find("test");

      objectMat = go.GetComponent<Renderer>().sharedMaterial;
      objectMat = material;
      go.GetComponent<Renderer>().sharedMaterial = material;
      }
      }

      public static void ChangeRenderMode(Material standardShaderMaterial, BlendMode blendMode)
      {
      switch (blendMode)
      {
      case BlendMode.Opaque:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
      standardShaderMaterial.SetInt("_ZWrite", 1);
      standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = -1;
      break;
      case BlendMode.Cutout:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
      standardShaderMaterial.SetInt("_ZWrite", 1);
      standardShaderMaterial.EnableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = 2450;
      break;
      case BlendMode.Fade:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
      standardShaderMaterial.SetInt("_ZWrite", 0);
      standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.EnableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = 3000;
      break;
      case BlendMode.Transparent:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
      standardShaderMaterial.SetInt("_ZWrite", 0);
      standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = 3000;
      break;
      }
      }
      }


      I want to make that each time I click the button it will change to the next mode. Opaque, Cutout, Fade, Transparent



      And after Transparent back to the first one Opaque



      The problem is that the method ChangeRenderMode get one specific mode.
      I want when clicking the button once ChangeRenderMode will get Opaque then next click it will get Cutout then Fade and Transparent.










      share|improve this question













      using System.Collections;
      using System.Collections.Generic;
      using UnityEditor;
      using UnityEngine;
      using UnityEditor.IMGUI;

      public class ChangeShaderRenderingModes : EditorWindow
      {
      public enum BlendMode
      {
      Opaque,
      Cutout,
      Fade,
      Transparent
      }

      static Material material;
      static Material objectMat;
      static int modeIndex = 0;

      [MenuItem("Tools/Change Rendering Modes")]
      public static void ShowWindow()
      {
      GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
      }

      private void OnGUI()
      {
      if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
      {
      var objects = Selection.objects;

      Shader shader = Shader.Find("Standard");
      material = new Material(shader);

      if (modeIndex == 4)
      modeIndex = 0;

      ChangeRenderMode(material, BlendMode.Opaque);

      GameObject go = GameObject.Find("test");

      objectMat = go.GetComponent<Renderer>().sharedMaterial;
      objectMat = material;
      go.GetComponent<Renderer>().sharedMaterial = material;
      }
      }

      public static void ChangeRenderMode(Material standardShaderMaterial, BlendMode blendMode)
      {
      switch (blendMode)
      {
      case BlendMode.Opaque:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
      standardShaderMaterial.SetInt("_ZWrite", 1);
      standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = -1;
      break;
      case BlendMode.Cutout:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
      standardShaderMaterial.SetInt("_ZWrite", 1);
      standardShaderMaterial.EnableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = 2450;
      break;
      case BlendMode.Fade:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
      standardShaderMaterial.SetInt("_ZWrite", 0);
      standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.EnableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = 3000;
      break;
      case BlendMode.Transparent:
      standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
      standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
      standardShaderMaterial.SetInt("_ZWrite", 0);
      standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
      standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
      standardShaderMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
      standardShaderMaterial.renderQueue = 3000;
      break;
      }
      }
      }


      I want to make that each time I click the button it will change to the next mode. Opaque, Cutout, Fade, Transparent



      And after Transparent back to the first one Opaque



      The problem is that the method ChangeRenderMode get one specific mode.
      I want when clicking the button once ChangeRenderMode will get Opaque then next click it will get Cutout then Fade and Transparent.







      c# unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:15









      Benzi Avrumi

      38610




      38610
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          enums start with default value of 0, increasing 1 per item, so



          public enum BlendMode
          {
          Opaque, // = 0
          Cutout, // = 1
          Fade, // = 2
          Transparent // = 3
          }


          So you can parse it into int calculate the next value and then parse it again to BlendMode:



          In your OnGUI function, replace



          if (modeIndex == 4)
          modeIndex = 0;
          ChangeRenderMode(material, BlendMode.Opaque);


          with



          var values = (BlendMode)Enum.GetValues(typeof(BlendMode))
          modeIndex = (BlendMode)((modeIndex+1) % values.Length);
          ChangeRenderMode(material, blendMode);





          share|improve this answer























          • Tried the modeIndex is 0 all the time after clicking the button.
            – Benzi Avrumi
            Nov 11 at 16:48












          • to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
            – derHugo
            Nov 11 at 17:18












          • @derHugo You are absolutely right
            – uɐʞɥsɐ
            Nov 11 at 17:19










          • It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
            – Benzi Avrumi
            Nov 11 at 19:26






          • 1




            it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
            – uɐʞɥsɐ
            Nov 11 at 19:41


















          up vote
          1
          down vote













          You can try to use a Dictionary key be current state value be next state.



          Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);


          and declare a value in the class to store BlendMode state.



          BlendMode currentMode = BlendMode.Cutout;


          Then you can use this currentMode (key) to get the next Mode.



          currentMode = dict[currentMode];


          Full code will be like



          public class ChangeShaderRenderingModes : EditorWindow
          {
          //store mode
          static BlendMode currentMode = BlendMode.Opaque;

          static Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();


          public void Awake()
          {
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);
          }


          public enum BlendMode
          {
          Opaque,
          Cutout,
          Fade,
          Transparent
          }

          static Material material;
          static Material objectMat;
          static int modeIndex = 0;

          [MenuItem("Tools/Change Rendering Modes")]
          public static void ShowWindow()
          {
          GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
          }

          private void OnGUI()
          {
          if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
          {
          var objects = Selection.objects;

          Shader shader = Shader.Find("Standard");
          material = new Material(shader);

          if (modeIndex == 4)
          modeIndex = 0;

          // take out the parameter
          ChangeRenderMode(material);

          GameObject go = GameObject.Find("test");

          objectMat = go.GetComponent<Renderer>().sharedMaterial;
          objectMat = material;
          go.GetComponent<Renderer>().sharedMaterial = material;
          }
          }

          public static void ChangeRenderMode(Material standardShaderMaterial)
          {
          switch (blendMode)
          {
          ....
          }
          // set next mode
          currentMode = dict[currentMode];
          }
          }


          NOTE



          This solution can be flexible to determine what the next Mode is.






          share|improve this answer























          • While this...functions, you introduce a point of code failure if the enum ever gets updated.
            – Draco18s
            Nov 11 at 17:31











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250651%2fhow-can-i-change-enum-items-using-a-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          enums start with default value of 0, increasing 1 per item, so



          public enum BlendMode
          {
          Opaque, // = 0
          Cutout, // = 1
          Fade, // = 2
          Transparent // = 3
          }


          So you can parse it into int calculate the next value and then parse it again to BlendMode:



          In your OnGUI function, replace



          if (modeIndex == 4)
          modeIndex = 0;
          ChangeRenderMode(material, BlendMode.Opaque);


          with



          var values = (BlendMode)Enum.GetValues(typeof(BlendMode))
          modeIndex = (BlendMode)((modeIndex+1) % values.Length);
          ChangeRenderMode(material, blendMode);





          share|improve this answer























          • Tried the modeIndex is 0 all the time after clicking the button.
            – Benzi Avrumi
            Nov 11 at 16:48












          • to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
            – derHugo
            Nov 11 at 17:18












          • @derHugo You are absolutely right
            – uɐʞɥsɐ
            Nov 11 at 17:19










          • It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
            – Benzi Avrumi
            Nov 11 at 19:26






          • 1




            it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
            – uɐʞɥsɐ
            Nov 11 at 19:41















          up vote
          2
          down vote



          accepted










          enums start with default value of 0, increasing 1 per item, so



          public enum BlendMode
          {
          Opaque, // = 0
          Cutout, // = 1
          Fade, // = 2
          Transparent // = 3
          }


          So you can parse it into int calculate the next value and then parse it again to BlendMode:



          In your OnGUI function, replace



          if (modeIndex == 4)
          modeIndex = 0;
          ChangeRenderMode(material, BlendMode.Opaque);


          with



          var values = (BlendMode)Enum.GetValues(typeof(BlendMode))
          modeIndex = (BlendMode)((modeIndex+1) % values.Length);
          ChangeRenderMode(material, blendMode);





          share|improve this answer























          • Tried the modeIndex is 0 all the time after clicking the button.
            – Benzi Avrumi
            Nov 11 at 16:48












          • to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
            – derHugo
            Nov 11 at 17:18












          • @derHugo You are absolutely right
            – uɐʞɥsɐ
            Nov 11 at 17:19










          • It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
            – Benzi Avrumi
            Nov 11 at 19:26






          • 1




            it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
            – uɐʞɥsɐ
            Nov 11 at 19:41













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          enums start with default value of 0, increasing 1 per item, so



          public enum BlendMode
          {
          Opaque, // = 0
          Cutout, // = 1
          Fade, // = 2
          Transparent // = 3
          }


          So you can parse it into int calculate the next value and then parse it again to BlendMode:



          In your OnGUI function, replace



          if (modeIndex == 4)
          modeIndex = 0;
          ChangeRenderMode(material, BlendMode.Opaque);


          with



          var values = (BlendMode)Enum.GetValues(typeof(BlendMode))
          modeIndex = (BlendMode)((modeIndex+1) % values.Length);
          ChangeRenderMode(material, blendMode);





          share|improve this answer














          enums start with default value of 0, increasing 1 per item, so



          public enum BlendMode
          {
          Opaque, // = 0
          Cutout, // = 1
          Fade, // = 2
          Transparent // = 3
          }


          So you can parse it into int calculate the next value and then parse it again to BlendMode:



          In your OnGUI function, replace



          if (modeIndex == 4)
          modeIndex = 0;
          ChangeRenderMode(material, BlendMode.Opaque);


          with



          var values = (BlendMode)Enum.GetValues(typeof(BlendMode))
          modeIndex = (BlendMode)((modeIndex+1) % values.Length);
          ChangeRenderMode(material, blendMode);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 17:22

























          answered Nov 11 at 16:18









          uɐʞɥsɐ

          19.7k1565114




          19.7k1565114












          • Tried the modeIndex is 0 all the time after clicking the button.
            – Benzi Avrumi
            Nov 11 at 16:48












          • to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
            – derHugo
            Nov 11 at 17:18












          • @derHugo You are absolutely right
            – uɐʞɥsɐ
            Nov 11 at 17:19










          • It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
            – Benzi Avrumi
            Nov 11 at 19:26






          • 1




            it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
            – uɐʞɥsɐ
            Nov 11 at 19:41


















          • Tried the modeIndex is 0 all the time after clicking the button.
            – Benzi Avrumi
            Nov 11 at 16:48












          • to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
            – derHugo
            Nov 11 at 17:18












          • @derHugo You are absolutely right
            – uɐʞɥsɐ
            Nov 11 at 17:19










          • It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
            – Benzi Avrumi
            Nov 11 at 19:26






          • 1




            it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
            – uɐʞɥsɐ
            Nov 11 at 19:41
















          Tried the modeIndex is 0 all the time after clicking the button.
          – Benzi Avrumi
          Nov 11 at 16:48






          Tried the modeIndex is 0 all the time after clicking the button.
          – Benzi Avrumi
          Nov 11 at 16:48














          to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
          – derHugo
          Nov 11 at 17:18






          to be more flexible and not rely on hardcoded indexes you could use var values = (BlendMode)Enum.GetValues(typeof(BlendMode)) to get all values and use modulo like modeIndex = (modeIndex+1) % values.Length;. I think that reads easy and requires two lines without if-else
          – derHugo
          Nov 11 at 17:18














          @derHugo You are absolutely right
          – uɐʞɥsɐ
          Nov 11 at 17:19




          @derHugo You are absolutely right
          – uɐʞɥsɐ
          Nov 11 at 17:19












          It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
          – Benzi Avrumi
          Nov 11 at 19:26




          It seems to be working now but there is another problem. When I click the button I see on my character the changes of each mode. But when I click on the inspector to expand the shader it's changing back automatic the mode to Opaque. When I click the button and it's changing mode it's making the shade in the inspector unexpended, So I click on the arrow to see what mode it is now and it's changing it back to Opaque. Is there anything to do about that ?
          – Benzi Avrumi
          Nov 11 at 19:26




          1




          1




          it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
          – uɐʞɥsɐ
          Nov 11 at 19:41




          it has nothing to do with this part of the code, may be somewhere else you are changing it, it resets the enum value.
          – uɐʞɥsɐ
          Nov 11 at 19:41












          up vote
          1
          down vote













          You can try to use a Dictionary key be current state value be next state.



          Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);


          and declare a value in the class to store BlendMode state.



          BlendMode currentMode = BlendMode.Cutout;


          Then you can use this currentMode (key) to get the next Mode.



          currentMode = dict[currentMode];


          Full code will be like



          public class ChangeShaderRenderingModes : EditorWindow
          {
          //store mode
          static BlendMode currentMode = BlendMode.Opaque;

          static Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();


          public void Awake()
          {
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);
          }


          public enum BlendMode
          {
          Opaque,
          Cutout,
          Fade,
          Transparent
          }

          static Material material;
          static Material objectMat;
          static int modeIndex = 0;

          [MenuItem("Tools/Change Rendering Modes")]
          public static void ShowWindow()
          {
          GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
          }

          private void OnGUI()
          {
          if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
          {
          var objects = Selection.objects;

          Shader shader = Shader.Find("Standard");
          material = new Material(shader);

          if (modeIndex == 4)
          modeIndex = 0;

          // take out the parameter
          ChangeRenderMode(material);

          GameObject go = GameObject.Find("test");

          objectMat = go.GetComponent<Renderer>().sharedMaterial;
          objectMat = material;
          go.GetComponent<Renderer>().sharedMaterial = material;
          }
          }

          public static void ChangeRenderMode(Material standardShaderMaterial)
          {
          switch (blendMode)
          {
          ....
          }
          // set next mode
          currentMode = dict[currentMode];
          }
          }


          NOTE



          This solution can be flexible to determine what the next Mode is.






          share|improve this answer























          • While this...functions, you introduce a point of code failure if the enum ever gets updated.
            – Draco18s
            Nov 11 at 17:31















          up vote
          1
          down vote













          You can try to use a Dictionary key be current state value be next state.



          Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);


          and declare a value in the class to store BlendMode state.



          BlendMode currentMode = BlendMode.Cutout;


          Then you can use this currentMode (key) to get the next Mode.



          currentMode = dict[currentMode];


          Full code will be like



          public class ChangeShaderRenderingModes : EditorWindow
          {
          //store mode
          static BlendMode currentMode = BlendMode.Opaque;

          static Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();


          public void Awake()
          {
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);
          }


          public enum BlendMode
          {
          Opaque,
          Cutout,
          Fade,
          Transparent
          }

          static Material material;
          static Material objectMat;
          static int modeIndex = 0;

          [MenuItem("Tools/Change Rendering Modes")]
          public static void ShowWindow()
          {
          GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
          }

          private void OnGUI()
          {
          if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
          {
          var objects = Selection.objects;

          Shader shader = Shader.Find("Standard");
          material = new Material(shader);

          if (modeIndex == 4)
          modeIndex = 0;

          // take out the parameter
          ChangeRenderMode(material);

          GameObject go = GameObject.Find("test");

          objectMat = go.GetComponent<Renderer>().sharedMaterial;
          objectMat = material;
          go.GetComponent<Renderer>().sharedMaterial = material;
          }
          }

          public static void ChangeRenderMode(Material standardShaderMaterial)
          {
          switch (blendMode)
          {
          ....
          }
          // set next mode
          currentMode = dict[currentMode];
          }
          }


          NOTE



          This solution can be flexible to determine what the next Mode is.






          share|improve this answer























          • While this...functions, you introduce a point of code failure if the enum ever gets updated.
            – Draco18s
            Nov 11 at 17:31













          up vote
          1
          down vote










          up vote
          1
          down vote









          You can try to use a Dictionary key be current state value be next state.



          Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);


          and declare a value in the class to store BlendMode state.



          BlendMode currentMode = BlendMode.Cutout;


          Then you can use this currentMode (key) to get the next Mode.



          currentMode = dict[currentMode];


          Full code will be like



          public class ChangeShaderRenderingModes : EditorWindow
          {
          //store mode
          static BlendMode currentMode = BlendMode.Opaque;

          static Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();


          public void Awake()
          {
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);
          }


          public enum BlendMode
          {
          Opaque,
          Cutout,
          Fade,
          Transparent
          }

          static Material material;
          static Material objectMat;
          static int modeIndex = 0;

          [MenuItem("Tools/Change Rendering Modes")]
          public static void ShowWindow()
          {
          GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
          }

          private void OnGUI()
          {
          if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
          {
          var objects = Selection.objects;

          Shader shader = Shader.Find("Standard");
          material = new Material(shader);

          if (modeIndex == 4)
          modeIndex = 0;

          // take out the parameter
          ChangeRenderMode(material);

          GameObject go = GameObject.Find("test");

          objectMat = go.GetComponent<Renderer>().sharedMaterial;
          objectMat = material;
          go.GetComponent<Renderer>().sharedMaterial = material;
          }
          }

          public static void ChangeRenderMode(Material standardShaderMaterial)
          {
          switch (blendMode)
          {
          ....
          }
          // set next mode
          currentMode = dict[currentMode];
          }
          }


          NOTE



          This solution can be flexible to determine what the next Mode is.






          share|improve this answer














          You can try to use a Dictionary key be current state value be next state.



          Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);


          and declare a value in the class to store BlendMode state.



          BlendMode currentMode = BlendMode.Cutout;


          Then you can use this currentMode (key) to get the next Mode.



          currentMode = dict[currentMode];


          Full code will be like



          public class ChangeShaderRenderingModes : EditorWindow
          {
          //store mode
          static BlendMode currentMode = BlendMode.Opaque;

          static Dictionary<BlendMode, BlendMode> dict = new Dictionary<BlendMode, BlendMode>();


          public void Awake()
          {
          dict.Add(BlendMode.Opaque, BlendMode.Cutout);
          dict.Add(BlendMode.Cutout, BlendMode.Fade);
          dict.Add(BlendMode.Fade, BlendMode.Transparent);
          dict.Add(BlendMode.Transparent, BlendMode.Opaque);
          }


          public enum BlendMode
          {
          Opaque,
          Cutout,
          Fade,
          Transparent
          }

          static Material material;
          static Material objectMat;
          static int modeIndex = 0;

          [MenuItem("Tools/Change Rendering Modes")]
          public static void ShowWindow()
          {
          GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
          }

          private void OnGUI()
          {
          if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
          {
          var objects = Selection.objects;

          Shader shader = Shader.Find("Standard");
          material = new Material(shader);

          if (modeIndex == 4)
          modeIndex = 0;

          // take out the parameter
          ChangeRenderMode(material);

          GameObject go = GameObject.Find("test");

          objectMat = go.GetComponent<Renderer>().sharedMaterial;
          objectMat = material;
          go.GetComponent<Renderer>().sharedMaterial = material;
          }
          }

          public static void ChangeRenderMode(Material standardShaderMaterial)
          {
          switch (blendMode)
          {
          ....
          }
          // set next mode
          currentMode = dict[currentMode];
          }
          }


          NOTE



          This solution can be flexible to determine what the next Mode is.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 16:36

























          answered Nov 11 at 16:29









          D-Shih

          24.6k61431




          24.6k61431












          • While this...functions, you introduce a point of code failure if the enum ever gets updated.
            – Draco18s
            Nov 11 at 17:31


















          • While this...functions, you introduce a point of code failure if the enum ever gets updated.
            – Draco18s
            Nov 11 at 17:31
















          While this...functions, you introduce a point of code failure if the enum ever gets updated.
          – Draco18s
          Nov 11 at 17:31




          While this...functions, you introduce a point of code failure if the enum ever gets updated.
          – Draco18s
          Nov 11 at 17:31


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250651%2fhow-can-i-change-enum-items-using-a-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ