UGUI 几个简单而实用的小脚本

RayCast检测

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace LargeScreen
{
    //只做点击使用,无渲染效果的Image
    public class NoDrawImage : Graphic
    {
        /// <summary>
        /// A utility class that can aid in the generation of meshes for the UI.
        /// </summary>
        /// <param name="vh">VertexHelper</param>
        protected override void OnPopulateMesh(VertexHelper vh)
        {         
            vh.Clear();
        }

#if UNITY_EDITOR
        [CustomEditor(typeof(NoDrawImage))]
        public class GraphicCastEditor : Editor
        {
            // Nothing show to Inspector
            public override void OnInspectorGUI() { }
        }
#endif
    }
}

图片旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace LargeScreen
{
    [DisallowMultipleComponent]
    public class TransFormXYZRotate : MonoBehaviour
    {
        private float fToatZ = 0;
        bool bForward = false;
        public float fRotateSpeed = 150f;
        [SerializeField]
        private float XAngle = 60f;
        private float YAngle = 0f;
        void Update()
        {
            ScoreRotate();
        }
        private void ScoreRotate()
        {
            fToatZ += Time.deltaTime * fRotateSpeed;
            if (fToatZ > 360f)
            {
                fToatZ = 0f;
            }
            if (bForward)
            {
                transform.localEulerAngles = new Vector3(XAngle, YAngle, fToatZ);
            }
            else
            {
                transform.localEulerAngles = new Vector3(XAngle, YAngle, -fToatZ);
            }

        }
    }

}

事件触发

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UEvent : EventTrigger
{
    public delegate void VoidDelegate();
    public delegate void VoidDelegateG(GameObject go);
    public delegate void VoidDelegate_(GameObject go, PointerEventData data);
    public delegate void VoidHoverDelegate(GameObject go, bool bHover);

    public VoidHoverDelegate OnHover;
    public VoidDelegateG onClick;
    public VoidDelegateG onDoubleClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegateG onSelect;
    public VoidDelegate onUpdateSelect;
    public VoidDelegate_ onClick_;
    public VoidDelegate_ onUp_;
    public VoidDelegate_ onEnter_;
    public VoidDelegate_ onExit_;

    static public UEvent Get(GameObject go)
    {
        UEvent listener = go.GetComponent<UEvent>();
        if (listener == null) listener = go.AddComponent<UEvent>();
        if(go.GetComponent<MaskableGraphic>())
        {
            go.GetComponent<MaskableGraphic>().raycastTarget = true;
        }
        return listener;
    }
    static public UEvent Get(Transform transform)
    {
        UEvent listener = transform.GetComponent<UEvent>();
        if (listener == null) listener = transform.gameObject.AddComponent<UEvent>();
        return listener;
    }

    public override void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left && eventData.clickCount == 2)
        {
            if (onDoubleClick != null)
            {
                onDoubleClick(gameObject);
            }
        }
        else if ( eventData.clickCount == 1)
        {
            if (onClick != null) onClick(gameObject);
            if (onClick_ != null) onClick_(gameObject, eventData);
        }
         
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown();
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (OnHover != null) OnHover(gameObject, true);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (OnHover != null) OnHover(gameObject, false);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp();
        if (onUp_ != null) onUp_(gameObject, eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect();
       
    }
}

呼吸效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class BreathLight : MonoBehaviour
{
    public Image[] breathLights;
    // Start is called before the first frame update
    void Start()
    {
        breathLights = this.transform.GetComponentsInChildren<Image>(true);
        for(int index = 0; index < breathLights.Length; index++ )
        {
            breathLights[index].DOFade(0, 2f).SetDelay(index).SetLoops(-1 , LoopType.Yoyo);
        }
    }

   
}

读取CSV配置文件

 #region 配置数据解析和处理接口
    Dictionary<string,MenuNodeData> PathConfigDataDic = new Dictionary<string, MenuNodeData>();
    private void InitPathCfg()
    {
        if (PathConfigDataDic == null)
        {
            PathConfigDataDic = new Dictionary<string, MenuNodeData>();
        }

        if (PathConfigDataDic.Count > 0)
        {
            return;
        }
   
        string[] paths = File.ReadAllLines(Application.streamingAssetsPath + "/Path.csv");
        for (int i = 1; i < paths.Length; i++)
        {
            string[] split = paths[i].Split(',');
            MenuNodeData item = new MenuNodeData();
            item.id = int.Parse(split[0]);
            item.key = split[1].Trim();
            item.name = split[2].Trim();        
            item.path = split[3].Trim();
            item.imgPath = split[4].Trim();
            PathConfigDataDic.Add(item.key, item);
        }
    }


    public MenuNodeData GetPathCfgByKey(string key)
    {
        InitPathCfg();
        MenuNodeData cfg = null;
        PathConfigDataDic.TryGetValue(key, out cfg);
        return cfg;
    }


public class MenuNodeData
{
    public int id;
    public string key;
    public string name;
    public string path;
    public string imgPath;

}

开启和杀掉进程

  #region 开启和杀掉进程

    public void StartProgramInvoke(string path)
    {
        KillProgram(CurProgramPath);
        CurProgramPath = path;
        CancelInvoke("StartProgramByPath");
        Invoke("StartProgramByPath", 0.5f);
    }



    private void StartProgramByPath()
    {
        if (string.IsNullOrEmpty(CurProgramPath))
        {
            return;
        }
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @CurProgramPath;
        startInfo.Arguments = "";
        Process pro = Process.Start(startInfo);
    }

    private void KillProgram(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            return;
        }

        string appName = Path.GetFileNameWithoutExtension(path);

        Process[] allProgresse =Process.GetProcessesByName(appName);
        foreach (Process closeProgress in allProgresse)
        {
            if (closeProgress.ProcessName.Equals(appName))
            {
                closeProgress.Kill();
                closeProgress.WaitForExit();
                break;
            }
        }
    }
    #endregion

帧率和分辨率设置

  private void InitSetting()
    {
        Application.targetFrameRate = 55;
        Screen.SetResolution(3080, 1080, true);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值