gpt4 book ai didi

c# - 如何使用按钮更改枚举项?

转载 作者:行者123 更新时间:2023-11-30 21:31:29 24 4
gpt4 key购买 nike

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;
}
}
}

我想让每次单击按钮时它都会切换到下一个模式。不透明、镂空、淡入淡出、透明

然后在Transparent之后回到第一个Opaque

问题在于 ChangeRenderMode 方法获得了一种特定模式。我希望当单击按钮时,一旦 ChangeRenderMode 将获得不透明,然后下一次单击它将获得 Cutout,然后是 Fade 和 Transparent。

最佳答案

枚举以默认值 0 开始,每项增加 1,因此

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

所以你可以把它解析成int计算下一个值然后再把它解析成BlendMode:

在您的 OnGUI 函数中,替换

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

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

关于c# - 如何使用按钮更改枚举项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com