using System;
using System.Globalization;
using UnityEngine;
using UnityObject = UnityEngine.Object;
#if INCLUDE_UGUI
using UnityEngine.UI;
#endif
namespace Unity.XR.CoreUtils
{
///
/// Runtime Material utilities.
///
public static class MaterialUtils
{
///
/// Clones and replaces the material assigned to a .
///
///
/// > [!WARNING]
/// > You must call on this material object when done.
///
///
/// The renderer assigned the material to clone.
/// The cloned material.
public static Material GetMaterialClone(Renderer renderer)
{
// The following is equivalent to renderer.material, but gets rid of the error messages in edit mode
return renderer.material = UnityObject.Instantiate(renderer.sharedMaterial);
}
#if INCLUDE_UGUI
///
/// Clones and replaces the material assigned to a .
///
///
/// To use this function, your project must contain the
/// [Unity UI package (com.unity.ugui)](https://docs.unity3d.com/Manual/com.unity.ugui.html).
///
/// > [!WARNING]
/// > You must call on this material object when done.
///
///
/// The Graphic object assigned the material to clone.
/// Cloned material
public static Material GetMaterialClone(Graphic graphic)
{
// The following is equivalent to graphic.material, but gets rid of the error messages in edit mode
return graphic.material = UnityObject.Instantiate(graphic.material);
}
#endif
///
/// Clones and replaces all materials assigned to a
///
///
/// > [!WARNING]
/// > You must call on each cloned material object in the array when done.
///
///
/// Renderer assigned the materials to clone and replace.
/// Cloned materials
public static Material[] CloneMaterials(Renderer renderer)
{
var sharedMaterials = renderer.sharedMaterials;
for (var i = 0; i < sharedMaterials.Length; i++)
{
sharedMaterials[i] = UnityObject.Instantiate(sharedMaterials[i]);
}
renderer.sharedMaterials = sharedMaterials;
return sharedMaterials;
}
///
/// Converts an RGB or RGBA formatted hex string to a object.
///
/// The formatted string, with an optional "0x" or "#" prefix.
/// The color value represented by the formatted string.
public static Color HexToColor(string hex)
{
hex = hex.Replace("0x", "").Replace("#", "");
var r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
var g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
var b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
var a = hex.Length == 8 ? byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber) : (byte)255;
return new Color32(r, g, b, a);
}
///
/// Shift the hue of a color by a given amount.
///
/// The hue value wraps around to 0 if the shifted hue exceeds 1.0.
/// The input color.
/// The amount of shift.
/// The output color.
public static Color HueShift(Color color, float shift)
{
Vector3 hsv;
Color.RGBToHSV(color, out hsv.x, out hsv.y, out hsv.z);
hsv.x = Mathf.Repeat(hsv.x + shift, 1f);
return Color.HSVToRGB(hsv.x, hsv.y, hsv.z);
}
///
/// Adds a material to this renderer's array of shared materials.
///
/// The renderer on which to add the material.
/// The material to add.
public static void AddMaterial(this Renderer renderer, Material material)
{
var materials = renderer.sharedMaterials;
var length = materials.Length;
var newMaterials = new Material[length + 1];
Array.Copy(materials, newMaterials, length);
newMaterials[length] = material;
renderer.sharedMaterials = newMaterials;
}
}
}