【Unity】简单功能脚本


单个操作


一、UI-不同状态的纯色颜色变换

适用范围举例:白色底色 且 nor状态不为白色的图片或按钮(hl press nor)

代码如下:

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

public class Skip_Color: MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerExitHandler //必须要继承的接口
{
    public Color EnterColor;//鼠标进入的颜色
    public Color DownColor;//鼠标按下的颜色
    public Color ExitColor;//鼠标退出的颜色

    private Image ThisImage;//脚本挂载物体的Image

    private void Start()
    {
        ThisImage = this.GetComponent<Image>();
    }

    //鼠标进入的函数
    public void OnPointerEnter(PointerEventData eventData)
    {
        ThisImage.color = EnterColor;
    }

    //鼠标按下的函数
    public void OnPointerDown(PointerEventData eventData)
    {
        ThisImage.color = DownColor;
    }

    //鼠标退出的函数
    public void OnPointerExit(PointerEventData eventData)
    {
        ThisImage.color = ExitColor;
    }
}

批量操作

一、创建控件

适用范围:设置父物体,控件生成数量。设置并勾选自定义控件及后缀,或勾选已定义的基础控件。

代码如下:

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

[ExecuteInEditMode]
public class CreateControls : MonoBehaviour {

    public Transform Parent;//生成控件的父物体
    public int CreateNum = 0;//生成控件的数量
    public int StartId = 0;//命名开始Id
    [Header("是否自定义模板")]
    public bool isManual = false;
    public GameObject ChildTemplate;
    public string ChildSuffix;
    [Space]
    [Header("内置模板单选")] 
    public bool isCreateButton = false;
    public bool isCreateImage = false;
    public bool isCreateText = false;
    [Space]  [Space] [Space]
    [Header("开始创建")]
    public bool StartCreate = false;

    private string firstControlName = null;//第一个控件
    private string lastControlName = null;//最后一个控件
    private string Suffix = null;//控件后缀

    private void Update()
    {
        if (StartCreate)
        {
            Debug.Log("开始创建控件");

            float beginTime = Time.realtimeSinceStartup;//开始时间记录	

            StartCreate_Control();
            StartCreate = false;

            float endTime = Time.realtimeSinceStartup;//结束时间记录	

            Debug.Log("控件创建完成");
            Debug.Log("共创建控件:" + CreateNum + "个," + "开始于控件:" + firstControlName + ",结束于控件:" + lastControlName + ",父物体为:" + Parent.name + ",耗费时间:" + (endTime - beginTime).ToString("f6") + "s");
        }
    }
   
    private void StartCreate_Control()
    {
        for(int i=0;i<CreateNum;i++)
        {
            GameObject newChild= Switch_Template();
            Suffix=Switch_Suffix();
            newChild.transform.SetParent(Parent,false);//设置worldpositionstay为false,避免缩放
            newChild.name = (i+ StartId) + Suffix;

            if (i == 0)
            {
                firstControlName = newChild.name;
            }
            else
            {
                lastControlName = newChild.name;
            }
        }
    }
    
    private string Switch_Suffix()//判断当前要创建的控件的后缀
    {
        if(isManual)
        {
            return ChildSuffix;
        }
        else if (isCreateButton)
        {
            return  "_btn";
        }
        else if (isCreateImage)
        {
            return "_img";
        }
        else if (isCreateText)
        {
            return "_txt";
        }
        return null;
    }

    private GameObject Switch_Template()//选择手动模板或内置模板
    {
        if (isManual)//手动模板
        {
            return Instantiate(ChildTemplate);
        }
        else//内置
        {
            return Switch_Type();
        }
    }

    private GameObject Switch_Type()//判断当前要创建的控件类别
    {
        if (isCreateButton)
        {
            return CreateButton();
        }
        else if (isCreateImage)
        {
            return CreateImage();
        }
        else if (isCreateText)
        {
            return CreateText();
        }
        return null;
    }

    private GameObject CreateButton()
    {
        GameObject Result = new GameObject("", typeof(Image));
        Result.AddComponent<Button>();
       
        return Result;
    }

    private GameObject CreateImage()
    {
        GameObject Result = new GameObject("", typeof(Image));
        Result.GetComponent<Image>().raycastTarget = false;

        return Result;
    }

    private GameObject CreateText()
    {
        GameObject Result = new GameObject("", typeof(Text));
        Result.GetComponent<Text>().raycastTarget = false;
        Suffix = "_txt";

        return Result;
    }
}

二、修改后缀

适用范围:自定义要修改的后缀和新后缀,并从根节点下的第一个子物体开始进行递归替换。

示例1:
修改前:物体A名称为:0_0_obj,物体B名称为:0_1obj,根节点:0_obj
旧后缀:_obj,新后缀:_txt
修改后:物体A名称为:0_0_txt,物体B名称为:0_1obj,根节点:0_obj

示例2:
修改前:物体A名称为:0_0_obj,物体B名称为:0_1obj,根节点:0_obj
旧后缀:obj,新后缀:_txt
修改后:物体A名称为:0_0__txt,物体B名称为:0_1_txt,根节点:0_obj

代码如下:

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

[ExecuteInEditMode]
public class ChangeSuffix : MonoBehaviour {
    public Transform RootNode;//根节点
    public string OldSuffix;//旧后缀
    public string NewSuffix;//新后缀
    [Space]
    [Space]
    [Space]
    public bool StartChange=false;//开始修改
    private int controlNum = 0; //后缀遍历个数
    private int changeNum = 0; //后缀修改个数
    private string firstControlName = null;//第一个控件
    private string lastControlName = null;//最后一个控件

    private void Update()
    {
        if (StartChange)
        {
            Debug.Log("开始修改后缀");

            float beginTime=Time.realtimeSinceStartup;//开始时间记录	

            Reset_Inform();
            StartChange_Suffix(RootNode);
            StartChange = false;

            float endTime = Time.realtimeSinceStartup;//结束时间记录	

            Debug.Log("后缀修改完成");
            Debug.Log("共遍历子控件:" + controlNum + "个," + "共修改子控件:" + changeNum + "个," + "开始于控件:" + firstControlName + ",结束于控件:" + lastControlName + ",耗费时间:" + (endTime - beginTime).ToString("f6") + "s");
        }
    }

    private void Reset_Inform()
    {
        controlNum = 0;
        changeNum = 0;
        firstControlName = null;
        lastControlName = null;
    }

    private void StartChange_Suffix(Transform rootNode)
    {
        foreach (Transform child in rootNode)
        {
            if (child.name.Length > OldSuffix.Length)//判断是否对当前物体命名进行修改
            {
                string nowSuffix = child.name.Substring(child.name.Length - OldSuffix.Length, OldSuffix.Length);//当前后缀
                string inform = child.name.Substring(0, child.name.Length - OldSuffix.Length);//去除后缀的其它信息

                if (nowSuffix == OldSuffix)
                {
                    changeNum++;
                    child.name = inform + NewSuffix;
                }
            }

            controlNum++;//统计信息更新
            if (controlNum == 1)
            {
                firstControlName = child.name;
            }
            else
            {
                lastControlName = child.name;
            }

            if (child.childCount != 0)//判断是否对下层子物体命名进行修改
            {
                StartChange_Suffix(child);
            }
        }         
    }
}


三、重命名

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

[ExecuteInEditMode]
public class BatchRenaming : MonoBehaviour {

    public Transform RootNode;//根节点
    public int StartId;//开始Id
    public string ConnectorString;//连接符
    public string Suffix;//后缀
    [Space]
    [Space]
    [Space]
    public bool StartChange = false;//开始修改
    private int controlNum = 0; //遍历个数
    private int changeNum = 0; //修改个数
    private string firstControlName = null;//第一个控件
    private string lastControlName = null;//最后一个控件
    private int NowId = 0;//当前Id
    private void Update()
    {
        if (StartChange)
        {
            Debug.Log("开始批量命名");

            float beginTime = Time.realtimeSinceStartup;//开始时间记录	

            Reset_Inform();
            Start_BatchRenaming(RootNode);
            StartChange = false;

            float endTime = Time.realtimeSinceStartup;//结束时间记录	

            Debug.Log("批量命名完成");
            Debug.Log("共遍历子控件:" + controlNum + "个," + "共修改子控件:" + changeNum + "个," + "开始于控件:" + firstControlName + ",结束于控件:" + lastControlName + ",耗费时间:" + (endTime - beginTime).ToString("f6") + "s");
        }
    }

    private void Reset_Inform()
    {
        controlNum = 0;
        changeNum = 0;
        firstControlName = null;
        lastControlName = null;
        NowId = StartId;
    }

    private void Start_BatchRenaming(Transform rootNode)
    {
        foreach (Transform child in rootNode)
        {
          
            changeNum++;
            child.name = NowId + ConnectorString+ Suffix;
            NowId++;

            controlNum++;//统计信息更新
            if (controlNum == 1)
            {
                firstControlName = child.name;
            }
            else
            {
                lastControlName = child.name;
            }
        }
    }
}


四、替换字体

适用范围:自定义要替换的新旧字体,并从根节点下的第一个子物体开始进行递归替换。

代码如下:

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

[ExecuteInEditMode]
public class ReplaceTheFont : MonoBehaviour
{
    public Transform RootNode;//根节点
    public Font OldFont;//旧字体
    public Font NewFont;//新字体
    [Space]
    [Space]
    [Space]
    public bool StartChange = false;//开始修改

    private int controlNum = 0; //后缀遍历个数
    private int changeNum = 0; //后缀修改个数
    private string firstControlName = null;//第一个控件
    private string lastControlName = null;//最后一个控件

    private void Update()
    {
        if (StartChange)
        {
            Debug.Log("开始替换字体");

            float beginTime = Time.realtimeSinceStartup;//开始时间记录	

            Reset_Inform();
            StartReplace_Font(RootNode);
            StartChange = false;

            float endTime = Time.realtimeSinceStartup;//结束时间记录	

            Debug.Log("字体替换完成");
            Debug.Log("共遍历子控件:" + controlNum + "个," + "共修改子控件:" + changeNum + "个," + "开始于控件:" + firstControlName + ",结束于控件:" + lastControlName + ",耗费时间:" + (endTime - beginTime).ToString("f6") + "s");
        }
    }

    private void Reset_Inform()
    {
        controlNum = 0;
        changeNum = 0;
        firstControlName = null;
        lastControlName = null;
    }

    private void StartReplace_Font(Transform rootNode)
    {
        foreach (Transform child in rootNode)
        {
            if (child.GetComponent<Text>())
            {
                if (child.GetComponent<Text>().font.name == OldFont.name)
                {
                    child.GetComponent<Text>().font = NewFont;
                    changeNum++;
                }
            }

            controlNum++;//统计信息更新
            if (controlNum == 1)
            {
                firstControlName = child.name;
            }
            else
            {
                lastControlName = child.name;
            }


            if (child.childCount != 0)//判断是否对下层子物体字体进行替换
            {
                StartReplace_Font(child);
            }
        }
    }
}

五、修改对应坐标

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

[ExecuteInEditMode]
public class SetPos : MonoBehaviour {

    public Transform RootNode1;//根节点-所有改变Pos的父节点
    public string ChangeSuffix;//该后缀的控件修改Pos

    public Transform RootNode2;//根节点-所有作为基值的父节点
    public string FixedSuffix;//该后缀的控件Pos作为基值

    public float X_DeviationValue, Y_DeviationValue;//偏移量

    public bool StartChange = false;//开始修改
    private int ControlNum = 0; //遍历个数
    private int ChangeNum = 0; //修改个数
    private string FirstControlName = null;//第一个控件
    private string LastControlName = null;//最后一个控件

    private void Update()
    {
        if (StartChange)
        {
            Debug.Log("开始批量修改对应坐标");

            float beginTime = Time.realtimeSinceStartup;//开始时间记录	

            Reset_Inform();
            Start_BatchRenaming(RootNode1, RootNode2);
            StartChange = false;

            float endTime = Time.realtimeSinceStartup;//结束时间记录	

            Debug.Log("批量修改对应坐标完成");
            Debug.Log("共遍历子控件:" + ControlNum + "个," + "共修改子控件:" + ChangeNum + "个," + "开始于控件:" + FirstControlName + ",结束于控件:" + LastControlName + ",耗费时间:" + (endTime - beginTime).ToString("f6") + "s");
        }
    }

    private void Reset_Inform()
    {
        ControlNum = 0;
        ChangeNum = 0;
        FirstControlName = null;
        LastControlName = null;
    }

    private void Start_BatchRenaming(Transform changeRootNode, Transform fixedRootNode)
    {
        int id = 0;
        foreach (Transform child in fixedRootNode)
        {
            Transform theChange = changeRootNode.GetChild(id); id++;
            if (child.name.Split('_')[1] == FixedSuffix)
            {             
                Vector3 fixedPos = child.position;
                Vector3 pos = new Vector3(fixedPos.x + X_DeviationValue, fixedPos.y + Y_DeviationValue, fixedPos.z);
                theChange.position = pos;

                ChangeNum++;
            }
                      
            ControlNum++;//统计信息更新
            if (ControlNum == 1)
            {
                FirstControlName = child.name;
            }
            else
            {
                LastControlName = child.name;
            }

            if (child.childCount != 0)//判断是否对下层子物体命名进行修改
            {
                Start_BatchRenaming(theChange,child);
            }
        }
    }
}

六、修改PC全盘自定义位置文件夹内文件名

using UnityEngine;
using System.IO;
//批量修改文件名
[ExecuteInEditMode]//不运行 仍可修改
public class ReName : MonoBehaviour
{
    public string FilesPath = @"D:\修改内容的上级";//绝对地址
    public string Suffix = ".mp3";
    public string TextToRemove = "删除内容";//删除原文件名中包含的相同内容
    public bool IsTest = true;//为true时,不进行实际修改,输出修改前后文件名
    public bool BeginChange = false;//为true时,进行实际修改

    private void Update()
    {
        if (BeginChange)
        {
            Debug.Log("开始修改文件名!!!");
            ChangFileName();
            BeginChange = false;
            Debug.Log("结束修改文件名!!!");
        }
    }

    private void ChangFileName()
    {
        Debug.Log("读取文件夹路径:" + FilesPath);
        Debug.Log("读取文件后缀:" + Suffix);
        Debug.Log("去除相同内容:"+ TextToRemove);

        string[] filePaths = Directory.GetFiles(FilesPath, "*" + Suffix);
        Debug.Log("初始读取文件数:"+ filePaths.Length);

        int num1 = 0, num2 = 0;
        foreach (string filePath in filePaths)
        {
            string oldFileName = Return_OldFileName(filePath);
            string tempFileName = Return_TempFileName(oldFileName);

            if (tempFileName.Split('(').Length > 1)
            {
                string newFileName = Return_NewFileName(tempFileName);

                string parentPath = Path.GetDirectoryName(filePath);
         
                string oldPath = parentPath + @"\"+oldFileName+ Suffix;
                string newPath = parentPath + @"\" + newFileName+Suffix;

                Debug.Log("修改正常,由 " + oldFileName + " 修改为 " + newFileName);
                if (!IsTest)
                {
                    Directory.Move(oldPath, newPath);
                }
          
                num1++;
            }
            else
            {
                num2++;
                Debug.Log("修改异常: " + oldFileName);
            }
        }

        Debug.Log("最终修改文件数:"+num1+"   异常文件数:"+num2);
    }

    private string Return_OldFileName(string filePath)//返回-原文件名
    {
        string temp = Path.GetFileName(filePath);
        string theSuffix = temp.Substring(temp.Length - Suffix.Length, Suffix.Length);

        string oldFileName = null;
        if (theSuffix == Suffix)
        {
            oldFileName =  temp.Substring(0, temp.Length - Suffix.Length);
        }
        else
        {
            oldFileName = temp;
        }

        return oldFileName;
    }

    private string Return_TempFileName(string oldFileName)//返回-临时文件名(原文件名,去除部分内容)
    {
        string tempFileName = oldFileName;

        if (!string.IsNullOrEmpty(TextToRemove))//字符串去除
        {
            tempFileName = tempFileName.Replace(TextToRemove, "");
        }

        return tempFileName;
    }

    private string Return_NewFileName(string tempFileName)//返回-新文件名
    {
        string newFileName = tempFileName.Split('(')[0];//新文件名 替换规则 按需修改即可
        
        return newFileName;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值