You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
112
Assets/Scripts/System/Utils/GraphicColorControl.cs
Normal file
112
Assets/Scripts/System/Utils/GraphicColorControl.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System.Collections.Generic;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace System.Utils
|
||||
{
|
||||
public class GraphicColorControl : MonoBehaviour
|
||||
{
|
||||
[SerializeField, BoxGroup ("ColorUtils"), Title ( "是否启用颜色混合模式")]
|
||||
private bool _isOverlyColor = true; //是否混合颜色
|
||||
|
||||
[SerializeField, BoxGroup ("ColorUtils"), Title ( "控制目标(可选,默认自动获取挂载对象)")]
|
||||
private Graphic _graphic;
|
||||
|
||||
[SerializeField, BoxGroup ("CustomColor"), Title ( "是否自定义混合底色"), ShowIf ("_isOverlyColor")]
|
||||
private bool _isCustomColor;
|
||||
|
||||
[SerializeField, BoxGroup ("CustomColor"), Title ( "自定义底色"), ShowIf ( "_isCustomColor")]
|
||||
private Color _customColor;
|
||||
|
||||
[SerializeField, BoxGroup ("CustomTarget"), Title ("扩展颜色控制目标")]
|
||||
private List<ColorTarget> _targets;
|
||||
|
||||
public Graphic Graphic
|
||||
{
|
||||
get => this._graphic;
|
||||
set => this._graphic = value;
|
||||
}
|
||||
|
||||
public bool IsCustomColor
|
||||
{
|
||||
get => this._isCustomColor;
|
||||
set => this._isCustomColor = value;
|
||||
}
|
||||
|
||||
public Color CustomColor
|
||||
{
|
||||
get => this._customColor;
|
||||
set => this._customColor = value;
|
||||
}
|
||||
|
||||
private Color _defaultColor;
|
||||
private bool _isInit;
|
||||
|
||||
private Color DefaultColor
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!this._isInit)
|
||||
{
|
||||
if (this._graphic == null)
|
||||
{
|
||||
this._graphic = this.GetComponent<Graphic> ();
|
||||
}
|
||||
|
||||
this._isInit = true;
|
||||
this._defaultColor = this._graphic.color;
|
||||
}
|
||||
|
||||
return this._isCustomColor ? this._customColor : this._defaultColor;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDefaultColor ()
|
||||
{
|
||||
var color = this.DefaultColor;
|
||||
this._graphic.color = color;
|
||||
if (this._targets?.Count > 0)
|
||||
{
|
||||
foreach (var target in this._targets)
|
||||
{
|
||||
if (target._graphic != null)
|
||||
{
|
||||
target._graphic.color = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetColor (Color qualityColor)
|
||||
{
|
||||
var defaultColor = this.DefaultColor;
|
||||
var color = qualityColor;
|
||||
this._graphic.color = this._isOverlyColor ? defaultColor * color : color;
|
||||
if (this._targets?.Count > 0)
|
||||
{
|
||||
foreach (var target in this._targets)
|
||||
{
|
||||
if (target._graphic != null)
|
||||
{
|
||||
target._graphic.color = target._isCustomColor ? color * target._customColor : color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class ColorTarget
|
||||
{
|
||||
[SerializeField, BoxGroup ("CustomColor"), Title ("控制目标")]
|
||||
public Graphic _graphic = null;
|
||||
|
||||
[SerializeField, BoxGroup ("CustomColor"), Title ("是否自定义混合底色")]
|
||||
public bool _isCustomColor = false;
|
||||
|
||||
[SerializeField, BoxGroup ("CustomColor"), Title ("自定义底色"), ShowIf ("_isCustomColor")]
|
||||
public Color _customColor = Color.white;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/System/Utils/GraphicColorControl.cs.meta
Normal file
3
Assets/Scripts/System/Utils/GraphicColorControl.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 549054882fba43cd9abb8261b43769aa
|
||||
timeCreated: 1689906776
|
||||
166
Assets/Scripts/System/Utils/ImageAdaptiveCropper.cs
Normal file
166
Assets/Scripts/System/Utils/ImageAdaptiveCropper.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace System.Utils
|
||||
{
|
||||
[AddComponentMenu ("UI/Tools/ImageAdaptiveCropper")]
|
||||
[RequireComponent (typeof(Image))]
|
||||
public class ImageAdaptiveCropper : MonoBehaviour
|
||||
{
|
||||
// private bool _hasHorizontal;
|
||||
[SerializeField] private Image _image;
|
||||
private RectTransform _rectTransform;
|
||||
private Texture2D _texture2D;
|
||||
#if UNITY_EDITOR
|
||||
[SerializeField] private Texture2D _preview;
|
||||
|
||||
[Button ("测试")]
|
||||
private void PreviewAdjust ()
|
||||
{
|
||||
SetAdjustTexture (this._preview);
|
||||
}
|
||||
#endif
|
||||
|
||||
public Image Image
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._image == null)
|
||||
{
|
||||
this._image = this.GetComponent<Image> ();
|
||||
}
|
||||
|
||||
return this._image;
|
||||
}
|
||||
}
|
||||
|
||||
private RectTransform RectTransform
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._rectTransform == null)
|
||||
{
|
||||
this._rectTransform = this.Image.transform as RectTransform;
|
||||
}
|
||||
|
||||
return this._rectTransform;
|
||||
}
|
||||
}
|
||||
|
||||
public Texture2D Texture2D
|
||||
{
|
||||
get => this._texture2D;
|
||||
// set => SetTexture (value);
|
||||
}
|
||||
|
||||
public void SetAdjustTexture (Texture2D texture)
|
||||
{
|
||||
if (texture == null)
|
||||
return;
|
||||
|
||||
var imageSize = this.RectTransform.sizeDelta;
|
||||
if (imageSize.x == 0 || imageSize.y == 0)
|
||||
return;
|
||||
|
||||
var imageWidth = Mathf.Abs ((imageSize.x));
|
||||
var imageHeight = Mathf.Abs ((imageSize.y));
|
||||
|
||||
var texture2DHeight = texture.height;
|
||||
var texture2DWidth = texture.width;
|
||||
|
||||
var newWidth = 0f;
|
||||
var newHeight = 0f;
|
||||
|
||||
if (imageWidth / imageHeight > texture2DWidth * 1f / texture2DHeight)
|
||||
{
|
||||
newHeight = texture2DWidth * (imageHeight / imageWidth);
|
||||
newWidth = newHeight * (imageWidth / imageHeight);
|
||||
if (newWidth > texture2DWidth)
|
||||
{
|
||||
var widthExOffset = newWidth / texture2DWidth;
|
||||
newWidth = texture2DWidth;
|
||||
newHeight = newHeight * (1 - widthExOffset);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newWidth = texture2DHeight * (imageWidth / imageHeight);
|
||||
newHeight = newWidth * (imageHeight / imageWidth);
|
||||
if (newHeight > texture2DHeight)
|
||||
{
|
||||
var heightExOffset = newHeight / texture2DHeight;
|
||||
newHeight = texture2DHeight;
|
||||
newWidth = newWidth * (1 - heightExOffset);
|
||||
}
|
||||
}
|
||||
|
||||
var offsetX = Mathf.FloorToInt ((texture2DWidth - newWidth) * 0.5f);
|
||||
var offsetY = Mathf.FloorToInt ((texture2DHeight - newHeight) * 0.5f);
|
||||
var textureCutOut = TextureCutOut (texture , offsetX , offsetY
|
||||
, newWidth , newHeight);
|
||||
this._texture2D = textureCutOut;
|
||||
this.Image.sprite = Sprite.Create (this._texture2D , new Rect (0 , 0 , this._texture2D.width , this._texture2D.height) , new Vector2 (0.5f , 0.5f));
|
||||
|
||||
}
|
||||
|
||||
public void SetTexture (Sprite sprite)
|
||||
{
|
||||
// this._texture2D = sprite.texture;
|
||||
this.Image.sprite = sprite;
|
||||
}
|
||||
|
||||
public void SetTextureAndAdjust (Sprite texture , Vector2 maskSize)
|
||||
{
|
||||
SetTexture (texture);
|
||||
// AdjustImage (maskSize);
|
||||
}
|
||||
|
||||
public void AdjustImage (Vector2 maskSize)
|
||||
{
|
||||
var imageSize = maskSize;
|
||||
if (imageSize.x == 0 || imageSize.y == 0)
|
||||
return;
|
||||
|
||||
var imageWidth = Mathf.Abs ((imageSize.x));
|
||||
var imageHeight = Mathf.Abs ((imageSize.y));
|
||||
|
||||
var texture2DHeight = this._texture2D.height;
|
||||
var texture2DWidth = this._texture2D.width;
|
||||
var rota = texture2DWidth * 1f / texture2DHeight;
|
||||
if (imageWidth / imageHeight > rota)
|
||||
{
|
||||
this.RectTransform.sizeDelta = new Vector2 (imageWidth , imageWidth / rota);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.RectTransform.sizeDelta = new Vector2 (imageHeight * rota, imageHeight);
|
||||
}
|
||||
|
||||
this.RectTransform.anchoredPosition = Vector2.zero;
|
||||
}
|
||||
|
||||
private Texture2D TextureCutOut (Texture2D originalTexture, int offsetX, int offsetY, float originalWidth, float originalHeight)
|
||||
{
|
||||
// originalTexture.
|
||||
Texture2D newTexture = new Texture2D (Mathf.CeilToInt (originalWidth), Mathf.CeilToInt (originalHeight));
|
||||
var pixels = originalTexture.GetPixels ();
|
||||
var originalTextureWidth = originalTexture.width;
|
||||
var originalTextureHeight = originalTexture.height;
|
||||
var colorList = new Color[newTexture.width * newTexture.height];
|
||||
for (int y = 0; y < newTexture.height; y++)
|
||||
{
|
||||
for (int x = 0; x < newTexture.width; x++)
|
||||
{
|
||||
newTexture.SetPixel (x, y, originalTexture.GetPixel (x + offsetX , y + offsetY));
|
||||
// var color = pixels[(y + offsetY) * originalTextureWidth + (x + offsetX)];
|
||||
// colorList[x * y] = color;
|
||||
}
|
||||
}
|
||||
newTexture.SetPixels (colorList);
|
||||
newTexture.anisoLevel = 2;
|
||||
newTexture.Apply ();
|
||||
return newTexture;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/System/Utils/ImageAdaptiveCropper.cs.meta
Normal file
3
Assets/Scripts/System/Utils/ImageAdaptiveCropper.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 705af729c68a46f282c04cd17048bbe9
|
||||
timeCreated: 1689841385
|
||||
67
Assets/Scripts/System/Utils/MultiGraphicControl.cs
Normal file
67
Assets/Scripts/System/Utils/MultiGraphicControl.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System.Collections.Generic;
|
||||
using System.RandomPool;
|
||||
using Game.Data;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace System.Utils
|
||||
{
|
||||
public class MultiGraphicControl : MonoBehaviour
|
||||
{
|
||||
[SerializeField, BoxGroup ("CustomTarget"), Title ("颜色控制目标")]
|
||||
public List<ColorTarget> _targets;
|
||||
|
||||
private List<Color> _defaultColors;
|
||||
private bool _isInit;
|
||||
|
||||
private Color DefaultColor (int index)
|
||||
{
|
||||
if (!this._isInit)
|
||||
{
|
||||
this._defaultColors = new List<Color> (this._targets.Count);
|
||||
foreach (var colorTarget in this._targets)
|
||||
{
|
||||
if (colorTarget._graphic == null)
|
||||
{
|
||||
this._defaultColors.Add (Color.white);
|
||||
}
|
||||
else
|
||||
{
|
||||
this._defaultColors.Add (colorTarget._graphic.color);
|
||||
}
|
||||
}
|
||||
|
||||
this._isInit = true;
|
||||
}
|
||||
|
||||
return this._defaultColors[index];
|
||||
}
|
||||
|
||||
public void SetColor (Color color)
|
||||
{
|
||||
if (this._targets?.Count > 0)
|
||||
{
|
||||
for (int i = 0; i < this._targets.Count; i++)
|
||||
{
|
||||
var target = this._targets[i];
|
||||
var defColor = this.DefaultColor(i);
|
||||
|
||||
if (target._graphic != null)
|
||||
{
|
||||
target._graphic.color = target._isCustomColor ? color * target._customColor : color * defColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void ResetDefaultColor ()
|
||||
{
|
||||
for (int i = 0; i < this._targets.Count; i++)
|
||||
{
|
||||
this._targets[i]._graphic.color = this.DefaultColor(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/System/Utils/MultiGraphicControl.cs.meta
Normal file
3
Assets/Scripts/System/Utils/MultiGraphicControl.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba68c1f55a7a4c25b203dacc510e029c
|
||||
timeCreated: 1689912085
|
||||
Reference in New Issue
Block a user