代码很简单没有难度,都有注解,随便 康一康 就会了。
Unity 屏幕截图
全屏截图方法
优点:响应速度快,几乎不用考虑优化问题。
缺点:只能截全屏。
IEnumerator Screenshot(string _ImageName)
{
yield return new WaitForEndOfFrame();
if (Directory.Exists(Environment.CurrentDirectory + "\\Screenshot ") == false)
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\Screenshot ");
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
else
{
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
yield break;
}
全屏截图方法 带委托事件
给两个参考文档: Texture2D.ReadPixels 参考文档
Rect类 参考文档
额。。。就是带了一个委托
IEnumerator Screenshot(Action _Action, string _ImageName)
{
yield return new WaitForEndOfFrame();
if (Directory.Exists(Environment.CurrentDirectory + "\\Screenshot ") == false)
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\Screenshot ");
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
else
{
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
_Action.Invoke();
yield break;
}
自定义截图方法
可自定义保存路径,截图名称,截图大小,就很银杏化
IEnumerator Screenshot(string _FilePath, string _ImageName,int _ImageWidth,int _ImageHeight)
{
yield return new WaitForEndOfFrame();
Texture2D _DestinationTexture;
_DestinationTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
if (_ImageWidth == 0)
{
_ImageWidth = Screen.width;
}
if (_ImageHeight == 0)
{
_ImageHeight = Screen.height;
}
Rect _RegionToReadFrom = new Rect(0, 0, _ImageWidth, _ImageHeight);
int xPosToWriteTo = 0;
int yPosToWriteTo = 0;
_DestinationTexture.ReadPixels(_RegionToReadFrom, xPosToWriteTo, yPosToWriteTo);
byte[] _BytesImage = _DestinationTexture.EncodeToPNG();
File.WriteAllBytes(_FilePath + "\\" + _ImageName + ".png", _BytesImage);
yield break;
}
自定义截图方法 带委托
没错 又是多了一个委托事件
IEnumerator Screenshot(Action _Action, string _FilePath, string _ImageName, int _ImageWidth, int _ImageHeight)
{
yield return new WaitForEndOfFrame();
Texture2D _DestinationTexture;
_DestinationTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
if (_ImageWidth == 0)
{
_ImageWidth = Screen.width;
}
if (_ImageHeight == 0)
{
_ImageHeight = Screen.height;
}
Rect _RegionToReadFrom = new Rect(0, 0, _ImageWidth, _ImageHeight);
int xPosToWriteTo = 0;
int yPosToWriteTo = 0;
_DestinationTexture.ReadPixels(_RegionToReadFrom, xPosToWriteTo, yPosToWriteTo);
byte[] _BytesImage = _DestinationTexture.EncodeToPNG();
File.WriteAllBytes(_FilePath + "\\" + _ImageName + ".png", _BytesImage);
_Action.Invoke();
yield break;
}
var foo = 'bar';
延迟工具
携程延迟方法
使用 协程特性实现程序等待
public IEnumerator DelayedTime(float _Time)
{
yield return new WaitForSeconds(_Time);
Debug.Log($"延迟{_Time}秒");
}
携程延迟带委托方法
和上面那个相似 不过加了个 委托事件
public IEnumerator DelayedTime(Action _Action, float _Time)
{
yield return new WaitForSeconds(_Time);
_Action.Invoke();
Debug.Log($"延迟{_Time}秒");
}
场景加载
场景加载 方法
是的 是场景加载
IEnumerator LoadSceneMap(string _StrMap)
{
SceneManager.LoadScene(_StrMap);
yield return null;
}
场景加载方法 带委托
没看错 就是简单的场景加载
IEnumerator LoadSceneMap(Action _ActionMaop,string _StrMap)
{
SceneManager.LoadScene(_StrMap);
yield return null;
}
异步场景加载 方法
异步场景加载 连委托都没有了
IEnumerator LoadSceneMap(string _StrMap, Slider _SliderLoad, Text _TextLoad)
{
AsyncOperation _Operation = SceneManager.LoadSceneAsync(_StrMap);
_Operation.allowSceneActivation = false;
while (!_Operation.isDone)
{
Debug.Log(_Operation.progress);
Debug.Log((_Operation.progress * 100).ToString() + "%");
_SliderLoad.value = Mathf.Lerp(_SliderLoad.value, _Operation.progress, Time.deltaTime * 1);
_TextLoad.text = (_Operation.progress * 100).ToString() + "%";
if (_Operation.progress >= 0.9f)
{
Debug.Log("100%");
_Operation.allowSceneActivation = true;
}
yield return null;
}
}
计时器方法
倒数计时器 附带委托 可自由变形
public IEnumerator TimerController(Action _ActionTimer, float _RefreshTime)
{
while (true)
{
_RefreshTime -= Time.deltaTime;
Debug.Log($"时间流逝{_RefreshTime}秒");
if (_RefreshTime <= 0)
{
_ActionTimer.Invoke();
yield break;
}
yield return null;
}
}
鼠标双击方法
算是对计时器的一个变种吧
public IEnumerator MouseClickDown(Action _ActionTimer, bool _BoolState)
{
float _StarTime = 0.0f;
float _EndTime = 0.0f;
int _Number = 0;
while (true)
{
if (Input.GetMouseButtonDown(0))
{
_Number++;
if (_Number == 1)
{
_StarTime = Time.realtimeSinceStartup;
}
else if (_Number >= 2)
{
_EndTime = Time.realtimeSinceStartup;
if (_EndTime - _StarTime <= 0.23f)
{
_ActionTimer.Invoke();
if (!_BoolState)
{
yield break;
}
}
_Number = 0;
}
}
yield return null;
}
}
最大最小值限定方法
是的呢 就是你看到的意思 啧... 真简单
public float Clam(float _Value, float _Min, float _Max)
{
if (_Value < _Min)
{
return _Min;
}
if (_Value > _Max)
{
return _Max;
}
return _Value;
}
完整代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CodeTools_ZH:MonoBehaviour
{
public static CodeTools_ZH _Instance;
private void Awake()
{
_Instance = this;
}
void Start()
{
StartCoroutine(DelayedTime(3.0f));
StartCoroutine(DelayedTime(() => { Debug.Log("方法延迟"); }, 4.0f));
StartCoroutine(TimerController(() => { Debug.Log("计时结束后的方法调用"); }, 4.0f));
StartCoroutine(MouseClickDown(() => { Debug.Log("鼠标双击事件"); }, false));
StartCoroutine(Screenshot( "全屏截图"));
StartCoroutine(Screenshot("F:\\桌面\\11","自定义截图",100,100));
StartCoroutine(Screenshot(()=> { Debug.Log("带委托的截图方法"); },"F:\\桌面\\11", "自定义截图", 100, 100));
Clam(12, 0, 50);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
}
}
#region 延迟响应
public IEnumerator DelayedTime(float _Time)
{
yield return new WaitForSeconds(_Time);
Debug.Log($"延迟{_Time}秒");
}
public IEnumerator DelayedTime(Action _Action, float _Time)
{
yield return new WaitForSeconds(_Time);
_Action.Invoke();
Debug.Log($"延迟{_Time}秒");
}
#endregion
#region 屏幕截图
IEnumerator Screenshot(string _ImageName)
{
yield return new WaitForEndOfFrame();
if (Directory.Exists(Environment.CurrentDirectory + "\\Screenshot ") == false)
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\Screenshot ");
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
else
{
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
yield break;
}
IEnumerator Screenshot(Action _Action, string _ImageName)
{
yield return new WaitForEndOfFrame();
if (Directory.Exists(Environment.CurrentDirectory + "\\Screenshot ") == false)
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\Screenshot ");
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
else
{
ScreenCapture.CaptureScreenshot(Environment.CurrentDirectory + "\\Screenshot\\" + _ImageName + ".png");
}
_Action.Invoke();
yield break;
}
IEnumerator Screenshot(string _FilePath, string _ImageName,int _ImageWidth,int _ImageHeight)
{
yield return new WaitForEndOfFrame();
Texture2D _DestinationTexture;
_DestinationTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
if (_ImageWidth == 0)
{
_ImageWidth = Screen.width;
}
if (_ImageHeight == 0)
{
_ImageHeight = Screen.height;
}
Rect _RegionToReadFrom = new Rect(0, 0, _ImageWidth, _ImageHeight);
int xPosToWriteTo = 0;
int yPosToWriteTo = 0;
_DestinationTexture.ReadPixels(_RegionToReadFrom, xPosToWriteTo, yPosToWriteTo);
byte[] _BytesImage = _DestinationTexture.EncodeToPNG();
File.WriteAllBytes(_FilePath + "\\" + _ImageName + ".png", _BytesImage);
yield break;
}
IEnumerator Screenshot(Action _Action, string _FilePath, string _ImageName, int _ImageWidth, int _ImageHeight)
{
yield return new WaitForEndOfFrame();
Texture2D _DestinationTexture;
_DestinationTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
if (_ImageWidth == 0)
{
_ImageWidth = Screen.width;
}
if (_ImageHeight == 0)
{
_ImageHeight = Screen.height;
}
Rect _RegionToReadFrom = new Rect(0, 0, _ImageWidth, _ImageHeight);
int xPosToWriteTo = 0;
int yPosToWriteTo = 0;
_DestinationTexture.ReadPixels(_RegionToReadFrom, xPosToWriteTo, yPosToWriteTo);
byte[] _BytesImage = _DestinationTexture.EncodeToPNG();
File.WriteAllBytes(_FilePath + "\\" + _ImageName + ".png", _BytesImage);
_Action.Invoke();
yield break;
}
#endregion
#region 场景加载
IEnumerator LoadSceneMap(string _StrMap, Slider _SliderLoad, Text _TextLoad)
{
AsyncOperation _Operation = SceneManager.LoadSceneAsync(_StrMap);
_Operation.allowSceneActivation = false;
while (!_Operation.isDone)
{
Debug.Log(_Operation.progress);
Debug.Log((_Operation.progress * 100).ToString() + "%");
_SliderLoad.value = Mathf.Lerp(_SliderLoad.value, _Operation.progress, Time.deltaTime * 1);
_TextLoad.text = (_Operation.progress * 100).ToString() + "%";
if (_Operation.progress >= 0.9f)
{
Debug.Log("100%");
_Operation.allowSceneActivation = true;
}
yield return null;
}
}
IEnumerator LoadSceneMap(Action _ActionMaop,string _StrMap)
{
SceneManager.LoadScene(_StrMap);
yield return null;
}
IEnumerator LoadSceneMap(string _StrMap)
{
SceneManager.LoadScene(_StrMap);
yield return null;
}
#endregion
public IEnumerator TimerController(Action _ActionTimer, float _RefreshTime)
{
while (true)
{
_RefreshTime -= Time.deltaTime;
Debug.Log($"时间流逝{_RefreshTime}秒");
if (_RefreshTime <= 0)
{
_ActionTimer.Invoke();
yield break;
}
yield return null;
}
}
public IEnumerator MouseClickDown(Action _ActionTimer, bool _BoolState)
{
float _StarTime = 0.0f;
float _EndTime = 0.0f;
int _Number = 0;
while (true)
{
if (Input.GetMouseButtonDown(0))
{
_Number++;
if (_Number == 1)
{
_StarTime = Time.realtimeSinceStartup;
}
else if (_Number >= 2)
{
_EndTime = Time.realtimeSinceStartup;
if (_EndTime - _StarTime <= 0.23f)
{
_ActionTimer.Invoke();
if (!_BoolState)
{
yield break;
}
}
_Number = 0;
}
}
yield return null;
}
}
public float Clam(float _Value, float _Min, float _Max)
{
if (_Value < _Min)
{
return _Min;
}
if (_Value > _Max)
{
return _Max;
}
return _Value;
}
}
暂时先这样吧,如果有时间的话就会更新,实在看不明白就留言,看到我会回复的。 路漫漫其修远兮,与君共勉。
|