短视频app搭建,unity Image透明度渐变
短视频app搭建,unity Image透明度渐变实现的相关代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Newtonsoft.Json; using UnityEditor; using UnityEditor.UI; //透明度渐变方法 public enum FadeMode { LeftTORight, UpToDown, LeftUpTORightDown, LeftDownTORightUp, } namespace UnityEngine.UI { public class ImageFade : Image { public FadeMode FadeMode; public byte FadeStartAlpha = 0; public byte FadeEndAlpha = 255; // public override void SetVerticesDirty() //{ // / base.SetVerticesDirty(); // } protected override void OnPopulateMesh(VertexHelper toFill) { base.OnPopulateMesh(toFill); UIVertex vertex = new UIVertex(); //print($"toFill.currentVertCount={toFill.currentIndexCount},toFill.currentVertCount={toFill.currentVertCount}"); for (int i = 0; i < toFill.currentVertCount; i++) { toFill.PopulateUIVertex(ref vertex, i); // print($"vertex[{i}] Position={vertex.position}"); // print($"vertex[{i}] Color={vertex.color}"); if (FadeMode == FadeMode.UpToDown) { if (i == 0 || i == 3) { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha); toFill.SetUIVertex(vertex, i); } else { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha); toFill.SetUIVertex(vertex, i); } } else if (FadeMode == FadeMode.LeftTORight) { if (i == 0 || i == 1) { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha); toFill.SetUIVertex(vertex, i); } else { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha); toFill.SetUIVertex(vertex, i); } } else if (FadeMode == FadeMode.LeftDownTORightUp) { if (i == 0) { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha); toFill.SetUIVertex(vertex, i); } else if (i == 1 || i == 3) { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, (byte)((FadeEndAlpha + FadeStartAlpha) / 2)); toFill.SetUIVertex(vertex, i); } else { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha); toFill.SetUIVertex(vertex, i); } } else if (FadeMode == FadeMode.LeftUpTORightDown) { if (i == 1) { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha); toFill.SetUIVertex(vertex, i); } else if (i == 0 || i == 2) { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, (byte)((FadeEndAlpha + FadeStartAlpha) / 2)); toFill.SetUIVertex(vertex, i); } else { vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha); toFill.SetUIVertex(vertex, i); } } } } } } //https://www.jianshu.com/p/035242c078c5?utm_campaign //https://blog.csdn.net/duzixi/article/details/78013295 //唯一作用显示公开字段 但是有你不想要的字段 [CustomEditor(typeof(ImageFade))] public class ImageFadeEditor : ImageEditor { protected SerializedProperty FadeMode; protected SerializedProperty FadeStartAlpha; protected SerializedProperty FadeEndAlpha; //https://blog.csdn.net/weixin_30360497/article/details/98998280 protected override void OnEnable() { base.OnEnable(); FadeMode = serializedObject.FindProperty("FadeMode");//得到类属性 FadeStartAlpha = serializedObject.FindProperty("FadeStartAlpha"); FadeEndAlpha = serializedObject.FindProperty("FadeEndAlpha"); } public override void OnInspectorGUI() { base.OnInspectorGUI(); serializedObject.Update();//更新序列化信息 EditorGUILayout.PropertyField(FadeMode); EditorGUILayout.PropertyField(FadeStartAlpha); EditorGUILayout.PropertyField(FadeEndAlpha); serializedObject.ApplyModifiedProperties();//应用属性改变 } }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
using UnityEditor;
using UnityEditor.UI;
//透明度渐变方法
public enum FadeMode
{
LeftTORight,
UpToDown,
LeftUpTORightDown,
LeftDownTORightUp,
}
namespace UnityEngine.UI
{
public class ImageFade : Image
{
public FadeMode FadeMode;
public byte FadeStartAlpha = 0;
public byte FadeEndAlpha = 255;
// public override void SetVerticesDirty()
//{
// / base.SetVerticesDirty();
// }
protected override void OnPopulateMesh(VertexHelper toFill)
{
base.OnPopulateMesh(toFill);
UIVertex vertex = new UIVertex();
//print($"toFill.currentVertCount={toFill.currentIndexCount},toFill.currentVertCount={toFill.currentVertCount}");
for (int i = 0; i < toFill.currentVertCount; i++)
{
toFill.PopulateUIVertex(ref vertex, i);
// print($"vertex[{i}] Position={vertex.position}");
// print($"vertex[{i}] Color={vertex.color}");
if (FadeMode == FadeMode.UpToDown)
{
if (i == 0 || i == 3)
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
toFill.SetUIVertex(vertex, i);
}
else
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
toFill.SetUIVertex(vertex, i);
}
}
else if (FadeMode == FadeMode.LeftTORight)
{
if (i == 0 || i == 1)
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
toFill.SetUIVertex(vertex, i);
}
else
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
toFill.SetUIVertex(vertex, i);
}
}
else if (FadeMode == FadeMode.LeftDownTORightUp)
{
if (i == 0)
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
toFill.SetUIVertex(vertex, i);
}
else if (i == 1 || i == 3)
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, (byte)((FadeEndAlpha + FadeStartAlpha) / 2));
toFill.SetUIVertex(vertex, i);
}
else
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
toFill.SetUIVertex(vertex, i);
}
}
else if (FadeMode == FadeMode.LeftUpTORightDown)
{
if (i == 1)
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
toFill.SetUIVertex(vertex, i);
}
else if (i == 0 || i == 2)
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, (byte)((FadeEndAlpha + FadeStartAlpha) / 2));
toFill.SetUIVertex(vertex, i);
}
else
{
vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
toFill.SetUIVertex(vertex, i);
}
}
}
}
}
}
//https://www.jianshu.com/p/035242c078c5?utm_campaign
//https://blog.csdn.net/duzixi/article/details/78013295
//唯一作用显示公开字段 但是有你不想要的字段
[CustomEditor(typeof(ImageFade))]
public class ImageFadeEditor : ImageEditor
{
protected SerializedProperty FadeMode;
protected SerializedProperty FadeStartAlpha;
protected SerializedProperty FadeEndAlpha;
//https://blog.csdn.net/weixin_30360497/article/details/98998280
protected override void OnEnable()
{
base.OnEnable();
FadeMode = serializedObject.FindProperty("FadeMode");//得到类属性
FadeStartAlpha = serializedObject.FindProperty("FadeStartAlpha");
FadeEndAlpha = serializedObject.FindProperty("FadeEndAlpha");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();//更新序列化信息
EditorGUILayout.PropertyField(FadeMode);
EditorGUILayout.PropertyField(FadeStartAlpha);
EditorGUILayout.PropertyField(FadeEndAlpha);
serializedObject.ApplyModifiedProperties();//应用属性改变
}
}
以上就是 短视频app搭建,unity Image透明度渐变实现的相关代码,更多内容欢迎关注之后的文章