我需要通过滑块更改对象的透明度
I need to change the transparency of the object through the slider
我有这个脚本来改变对象的透明度,它最初是关闭的,当我点击打开它的按钮时,它的克隆出现了......但是当我打开这个对象时,我无法改变透明度...统一没有错误,只是透明度没有改变...
public class ChangeOpacity : MonoBehaviour
{
public GameObject currentGameObject;
private AsistantControll AsistantControllScript;
public float alpha = 0.5f;//half transparency
//Get current material
private Material currentMat;
// Start is called before the first frame update
void Start()
{
AsistantControllScript = FindObjectOfType<AsistantControll>();
currentMat = currentGameObject.GetComponent<Renderer>().material;
}
// Update is called once per frame
void Update()
{
}
void ChangeAlpha(Material mat, float alphaVal)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
mat.SetColor("_Color", newColor);
}
public void ChangeAlphaOnValue(Slider slider)
{
ChangeAlpha(currentMat, slider.value);
}
}
你的代码中有些地方是错误的,或者你没有写完所有的代码。
从哪里调用 ChangeAlphaValue 方法?将哪个滑块作为参数传递给它?
您应该在脚本中添加滑块的序列化字段并在检查器中引用它。
[SerializeField] private Slider m_slider = null;
void Start()
{
...
m_slider.onValueChanged.AddListener(ChangeAlphaOnValue);
}
void OnDestroy()
{
m_slider.onValueChanged.RemoveListener(ChangeAlphaOnValue);
}
public void ChangeAlphaOnValue(float value)
{
ChangeAlpha(currentMat, value);
}
使用该代码,只要您的滑块更改值(当您在运行时拖动它或通过代码设置它时),就会调用您的 ChangeAlphaOnValue。
我有这个脚本来改变对象的透明度,它最初是关闭的,当我点击打开它的按钮时,它的克隆出现了......但是当我打开这个对象时,我无法改变透明度...统一没有错误,只是透明度没有改变...
public class ChangeOpacity : MonoBehaviour
{
public GameObject currentGameObject;
private AsistantControll AsistantControllScript;
public float alpha = 0.5f;//half transparency
//Get current material
private Material currentMat;
// Start is called before the first frame update
void Start()
{
AsistantControllScript = FindObjectOfType<AsistantControll>();
currentMat = currentGameObject.GetComponent<Renderer>().material;
}
// Update is called once per frame
void Update()
{
}
void ChangeAlpha(Material mat, float alphaVal)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
mat.SetColor("_Color", newColor);
}
public void ChangeAlphaOnValue(Slider slider)
{
ChangeAlpha(currentMat, slider.value);
}
}
你的代码中有些地方是错误的,或者你没有写完所有的代码。
从哪里调用 ChangeAlphaValue 方法?将哪个滑块作为参数传递给它?
您应该在脚本中添加滑块的序列化字段并在检查器中引用它。
[SerializeField] private Slider m_slider = null;
void Start()
{
...
m_slider.onValueChanged.AddListener(ChangeAlphaOnValue);
}
void OnDestroy()
{
m_slider.onValueChanged.RemoveListener(ChangeAlphaOnValue);
}
public void ChangeAlphaOnValue(float value)
{
ChangeAlpha(currentMat, value);
}
使用该代码,只要您的滑块更改值(当您在运行时拖动它或通过代码设置它时),就会调用您的 ChangeAlphaOnValue。