- Home /
Modify skybox material property in-game via script
Hi everyone,
I'm quite new to Unity and C# and I'm trying to figure out how to modify (+/-) a custom skybox property ('azimuth', adjusts sun positioning) in real time using a keypress during gameplay. While I understand how to get code to trigger on input, I'm not sure how to access and modify the various parameters of skybox materials.
I've attached a portion of code I found within the skybox editor script which may or may not be relevant.
EditorGUILayout.Space ();
var az = GetMaterialProperty (targets, "_SunAzimuth");
var al = GetMaterialProperty (targets, "_SunAltitude");
if (az.hasMixedValue || al.hasMixedValue )
{
EditorGUILayout.HelpBox ("Editing angles is disabled because they have mixed values.", MessageType.Warning);
}
else
{
FloatProperty (az, "Azimuth");
FloatProperty (al, "Altitude");
}
Many thanks for the help! I've been at this for hours with no luck. :(
Jonathan
Answer by mbbmbbmm · Jul 01, 2017 at 10:25 PM
using UnityEngine;
public class ChangeSunVector : MonoBehaviour
{
public float azimuth;
public float altitude;
Material skyMat;
Vector4 upVector;
void Start()
{
skyMat = RenderSettings.skybox;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
skyMat.SetVector("_SunVector", CalculateSunVector());
}
}
Vector4 CalculateSunVector()
{
var raz = azimuth * Mathf.Deg2Rad;
var ral = altitude * Mathf.Deg2Rad;
return new Vector4(
Mathf.Cos(ral) * Mathf.Sin(raz),
Mathf.Sin(ral),
Mathf.Cos(ral) * Mathf.Cos(raz),
0.0f
);
}
}
@jonathanbaird don't know if you still need it..
Your answer
Follow this Question
Related Questions
Singleton's property is always null 2 Answers
How to create new materials at runtime? 1 Answer
Directly assigning Material through Renderer.materials doesn't work 2 Answers
Blink Emission 0 Answers