| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using UnityEngine.UI;
- namespace TheraBytes.BetterUi
- {
- [HelpURL("https://documentation.therabytes.de/better-ui/BetterRawImage.html")]
- [AddComponentMenu("Better UI/Controls/Better Raw Image", 30)]
- public class BetterRawImage : RawImage, IImageAppearanceProvider, IResolutionDependency
- {
- #region Nested Types
- [Serializable]
- public class TextureSettings : IScreenConfigConnection
- {
- public Texture Texture;
- public ColorMode ColorMode;
- public Color PrimaryColor;
- public Color SecondaryColor;
- public Rect UvRect;
- [SerializeField]
- string screenConfigName;
- public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
- public TextureSettings(Texture texture, ColorMode colorMode, Color primary, Color secondary, Rect uvRect)
- {
- this.Texture = texture;
- this.ColorMode = colorMode;
- this.PrimaryColor = primary;
- this.SecondaryColor = secondary;
- this.UvRect = uvRect;
- }
- }
- [Serializable]
- public class TextureSettingsConfigCollection : SizeConfigCollection<TextureSettings> { }
- #endregion
- public string MaterialType
- {
- get { return materialType; }
- set { ImageAppearanceProviderHelper.SetMaterialType(value, this, materialProperties, ref materialEffect, ref materialType); }
- }
- public MaterialEffect MaterialEffect
- {
- get { return materialEffect; }
- set { ImageAppearanceProviderHelper.SetMaterialEffect(value, this, materialProperties, ref materialEffect, ref materialType); }
- }
- public VertexMaterialData MaterialProperties { get { return materialProperties; } }
- public ColorMode ColoringMode
- {
- get { return colorMode; }
- set
- {
- Config.Set(value, (o) => colorMode = value, (o) => CurrentTextureSettings.ColorMode = value);
- SetVerticesDirty();
- }
- }
- public Color SecondColor
- {
- get { return secondColor; }
- set
- {
- Config.Set(value, (o) => secondColor = value, (o) => CurrentTextureSettings.SecondaryColor = value);
- SetVerticesDirty();
- }
- }
- public override Color color
- {
- get { return base.color; }
- set
- {
- Config.Set(value, (o) => base.color = value, (o) => CurrentTextureSettings.PrimaryColor = value);
- }
- }
- public new Texture texture
- {
- get { return base.texture; }
- set
- {
- Config.Set(value, (o) => base.texture = value, (o) => CurrentTextureSettings.Texture = value);
- }
- }
- public new Rect uvRect
- {
- get { return base.uvRect; }
- set
- {
- Config.Set(value, (o) => base.uvRect = value, (o) => CurrentTextureSettings.UvRect = value);
- }
- }
- [SerializeField]
- ColorMode colorMode = ColorMode.Color;
- [SerializeField]
- Color secondColor = Color.white;
- [SerializeField]
- VertexMaterialData materialProperties = new VertexMaterialData();
- [SerializeField]
- string materialType;
- [SerializeField]
- MaterialEffect materialEffect;
- [SerializeField]
- float materialProperty1, materialProperty2, materialProperty3;
- [SerializeField]
- TextureSettings fallbackTextureSettings;
- [SerializeField]
- TextureSettingsConfigCollection customTextureSettings = new TextureSettingsConfigCollection();
- public TextureSettings CurrentTextureSettings
- {
- get
- {
- DoValidation();
- return customTextureSettings.GetCurrentItem(fallbackTextureSettings);
- }
- }
- protected override void OnEnable()
- {
- base.OnEnable();
- AssignTextureSettings();
- if (MaterialProperties.FloatProperties != null)
- {
- if (MaterialProperties.FloatProperties.Length > 0)
- materialProperty1 = MaterialProperties.FloatProperties[0].Value;
- if (MaterialProperties.FloatProperties.Length > 1)
- materialProperty2 = MaterialProperties.FloatProperties[1].Value;
- if (MaterialProperties.FloatProperties.Length > 2)
- materialProperty3 = MaterialProperties.FloatProperties[2].Value;
- }
- }
- public float GetMaterialPropertyValue(int propertyIndex)
- {
- return ImageAppearanceProviderHelper.GetMaterialPropertyValue(propertyIndex,
- ref materialProperty1, ref materialProperty2, ref materialProperty3);
- }
- public void SetMaterialProperty(int propertyIndex, float value)
- {
- ImageAppearanceProviderHelper.SetMaterialProperty(propertyIndex, value, this, materialProperties,
- ref materialProperty1, ref materialProperty2, ref materialProperty3);
- }
- protected override void OnPopulateMesh(VertexHelper vh)
- {
- Rect rect = GetPixelAdjustedRect();
- Vector2 pMin = new Vector2(rect.x, rect.y);
- Vector2 pMax = new Vector2(rect.x + rect.width, rect.y + rect.height);
- float w = (texture != null) ? (float)texture.width * texture.texelSize.x : 1;
- float h = (texture != null) ? (float)texture.height * texture.texelSize.y : 1;
- Vector2 uvMin = new Vector2(this.uvRect.xMin * w, this.uvRect.yMin * h);
- Vector2 uvMax = new Vector2(this.uvRect.xMax * w, this.uvRect.yMax * h);
- vh.Clear();
- ImageAppearanceProviderHelper.AddQuad(vh, rect,
- pMin, pMax,
- colorMode, color, secondColor,
- uvMin, uvMax,
- materialProperties);
- }
- public void OnResolutionChanged()
- {
- AssignTextureSettings();
- }
- private void AssignTextureSettings()
- {
- var settings = CurrentTextureSettings;
- this.texture = settings.Texture;
- this.colorMode = settings.ColorMode;
- this.color = settings.PrimaryColor;
- this.secondColor = settings.SecondaryColor;
- this.uvRect = settings.UvRect;
- }
- #if UNITY_EDITOR
- protected override void OnValidate()
- {
- base.OnValidate();
- DoValidation();
- AssignTextureSettings();
- }
- #endif
- void DoValidation()
- {
- bool isUnInitialized = fallbackTextureSettings == null
- || (fallbackTextureSettings.Texture == null
- && fallbackTextureSettings.ColorMode == ColorMode.Color
- && fallbackTextureSettings.PrimaryColor == new Color()
- && uvRect == new Rect());
- if (isUnInitialized)
- {
- fallbackTextureSettings = new TextureSettings(this.texture, this.colorMode, this.color, this.secondColor, this.uvRect);
- }
- }
- }
- }
|