常用功能整理记录(一些比较简单的功能实现集合(网上找的一些比较好的解决方案)放在这里)
目录
控制台打印
深拷贝
物体跟随鼠标移动
UI跟随玩家移动(Canvas在屏幕坐标覆盖或者世界坐标皆可)
简易存档工具
Sprite字体使用
防止鼠标射线穿透UI的API
UniWebview无法加载Https解决办法
AB包加载小工具
Singleton
Unity物体看向鼠标
Unity小程序发布
GoogleProtobuf使用学习链接
Adrressable教程
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ConsoleToSceen : MonoBehaviour
{const int maxLines = 50;const int maxLineLength = 120;private string _logStr = "";private readonly List
}
public static T DeepCopyByReflection
private Vector3 cubeScreenPos;private Vector3 mouseScreenPos;private Vector3 objOffset;// 偏移private void OnMouseDown(){Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);mouseScreenPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z);objOffset = transform.position - Camera.main.ScreenToWorldPoint(mouseScreenPos);}private void OnMouseDrag(){//为什么要先把cube转化为屏幕坐标?这样可以保证cube的z轴坐标不变cubeScreenPos = Camera.main.WorldToScreenPoint(transform.position);mouseScreenPos.x = Input.mousePosition.x;mouseScreenPos.y = Input.mousePosition.y;mouseScreenPos.z = cubeScreenPos.z;Vector3 endPos = Camera.main.ScreenToWorldPoint(mouseScreenPos) + objOffset;// transform.Translate(new Vector3(endPos.x, transform.position.y, transform.position.z) * Time.deltaTime, Space.World);//transform.Translate(new Vector3(endPos.x, endPos.y, endPos.z) * Time.deltaTime, Space.World);transform.position = new Vector3(endPos.x, transform.position.y, transform.position.z);// transform.}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIContraller : MonoBehaviour
{public GameObject player;public Camera cam;// Update is called once per framevoid Update(){CalPosition();// Vector2 pos = Camera.main.WorldToScreenPoint(player.transform.position);// GetComponent
控制UI显示顺序:UI是根据摄像机控制的,canvas下的图层顺序没用,控制摄像机的深度值即可设置Canvas显示深度
世界坐标的Canvas和世界坐标比例100:1,设置Canvas缩放0.01,直接设置坐标即可
go.GetComponent
VSCode新版代码不提示:
搜索Snippet,settingJson中修改"editor.suggest.snippetsPreventQuickSuggestions": false
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
public static class SaveJson
{public static void WriteToJson(string fileName, object data){var json = JsonUtility.ToJson(data, true);Debug.Log(json);var path = Path.Combine(Application.persistentDataPath, fileName);try{File.WriteAllText(path, json);}catch (System.Exception e){Debug.LogError("未能写入到该json文件" + e);}}public static T ReadJsonFile
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
public class SetTTf : MonoBehaviour
{//本方法是通过裁切的sprite导出字体文件,裁切使用的是unity自带的sprite editor,方便操作。//另外,裁切之后,每个sprite的名字的最后一个字符对应了ascii码的编码,比如://0: 我们只要将sprite的名字命名成xxx0,就可以了!//由于使用到的了sprite加载,所以字体图片请放在Resources目录下面,等制作完毕,再把他们放到fonts文件夹或者其他文件夹中即可。[MenuItem("SetTTf/CreateMyFontSprite")]static void CreateMyFontSprite(){if (Selection.objects == null) return;if (Selection.objects.Length == 0){Debug.LogWarning("没有选中Sprite文件,需要将Sprite Mode设置成Multiple,切分好,并且以以名字的最后一个字符当做ascii码");return;}string resoursePath = "Resources";Object o = Selection.objects[0];if (o.GetType() != typeof(Texture2D)){Debug.LogWarning("选中的并不是图片文件");return;}string selectiOnPath= AssetDatabase.GetAssetPath(o);if (selectionPath.Contains(resoursePath)){string selectiOnExt= Path.GetExtension(selectionPath);if (selectionExt.Length == 0){return;}string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);string fOntPathName= loadPath + ".fontsettings";string matPathName = loadPath + ".mat";float lineSpace = 0.1f;//字体行间距,下面会根据最高的字体得到行间距,如果是固定高度,可以在这里自行调整loadPath = Path.GetFileNameWithoutExtension(selectionPath);Sprite[] sprites = Resources.LoadAll
}
if (!EventSystem.current.IsPointerOverGameObject()){//}
网址https://blog.csdn.net/boyZhenGui/article/details/121250008UniWebView,是一个支持安卓,ios和mac系统的网页插件(Windows不可用,意味着Windows系统的Unity编辑器无法看到效果,只有打包放到手机上才可以看到效果)。原因
出现ERR_CLEARTEXT_NOT_PERMITTED这个报错的原因,在于安卓。从Android 9.0(API级别28)开始,默认情况下禁用明文支持。因此http的url均无法在webview中加载。解决方案
所以我们需要在安卓的清单中AndroidManifest.xml,添加可以加载http的标签。在Unity中完成修改AndroidManifest.xml
在Unity中无AndroidManifest.xml文件的情况下。Unity默认情况会根据插件中的所有 Android 清单,和打包设置,自动生成一份清单文件AndroidManifest.xml。如果Unity中Assets/Plugins/Android/AndroidManifest.xml,有清单文件,则会使用此清单。对于在Unity中的安卓清单详细解释可以看Unity官方手册。网上找到的清单都不符合Unity需求,我们需要得到Unity需要的清单文件。一、打包apk,得到Unity自动生成的最终清单文件AndroidManifest.xml
打包成功apk后,在关闭Unity编辑器之前,在Temp/StagingArea/AndroidManifest.xml 可以找到此apk使用的最终清单。二、将最终清单文件放到Unity中
复制步骤一中得到的清单,放到如下链接Assets/Plugins/Android/AndroidManifest.xml。三、修改AndroidManifest.xml
打开AndroidManifest.xml,找到标签, 在里面加入android:usesCleartextTraffic="true"即可,这个代码标表示可以使用http明文传输,解决方法:找到AndroidManifest.xml的方法=“找到Player下安卓的发布设置找到custom mina manifest”;或者在unity主程序找相同文件复制
ab包的名称/// 资源名称public T LoadResource ab包名称/// 资源名称public void LoadResourceAsync(string abName, string resName, System.Action finishLoadObjectHandler){AssetBundle ab = LoadABPackage(abName);//开启协程 提供资源加载成功后的委托StartCoroutine(LoadRes(ab, resName, finishLoadObjectHandler));}private IEnumerator LoadRes(AssetBundle ab, string resName, System.Action finishLoadObjectHandler){if (ab == null) yield break;//异步加载资源APIAssetBundleRequest abr = ab.LoadAssetAsync(resName);yield return abr;//委托调用处理逻辑finishLoadObjectHandler(abr.asset);}//根据Type异步加载资源public void LoadResourceAsync(string abName, string resName, System.Type type, System.Action finishLoadObjectHandler){AssetBundle ab = LoadABPackage(abName);StartCoroutine(LoadRes(ab, resName, type, finishLoadObjectHandler));}private IEnumerator LoadRes(AssetBundle ab, string resName, System.Type type, System.Action finishLoadObjectHandler){if (ab == null) yield break;AssetBundleRequest abr = ab.LoadAssetAsync(resName, type);yield return abr;//委托调用处理逻辑finishLoadObjectHandler(abr.asset);}//泛型加载public void LoadResourceAsync
using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;///
#if UNITY_EDITOR || UNITY_STANDALONEreturn Application.dataPath + "/StreamingAssets/";
#elif UNITY_IPHONEreturn Application.dataPath + "/Raw/";
#elif UNITY_ANDROIDreturn Application.dataPath + "!/assets/";
#endif}}//各个平台下的主包名称 --- 用以加载主包获取依赖信息private string mainABName{get{
#if UNITY_EDITOR || UNITY_STANDALONEreturn "StandaloneWindows";
#elif UNITY_IPHONEreturn "IOS";
#elif UNITY_ANDROIDreturn "Android";
#endif}}//加载AB包private AssetBundle LoadABPackage(string abName){AssetBundle ab;//加载ab包,需一并加载其依赖包。if (mainAB == null){//根据各个平台下的基础路径和主包名加载主包mainAB = AssetBundle.LoadFromFile(basePath + mainABName);//获取主包下的AssetBundleManifest资源文件(存有依赖信息)mainManifest = mainAB.LoadAsset
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 无需挂载直接调用即可
///
///
public class Singleton
{private static T instance;public static T Instance{get{if (instance == null){instance = FindObjectOfType(typeof(T)) as T;if (instance == null){GameObject go = new GameObject();go.name = typeof(T).ToString();instance = go.AddComponent
public LayerMask whatIsGround;// Update is called once per framevoid Update(){GetPos();}void GetPos(){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hitInfo;if (Physics.Raycast(ray, out hitInfo, 200, whatIsGround)){Vector3 target = hitInfo.point;target.y = transform.position.y;transform.LookAt(target);}}
Unity对接抖音_暗夜血媚的博客-CSDN博客_unity 抖音小游戏
protobuf如何使用Protogen工具生成CS代码详细教学篇_Little丶Seven的博客-CSDN博客_protobuf protogen
【游戏开发探究】Unity Addressables资源管理方式用起来太爽了,资源打包、加载、热更变得如此轻松(Addressable Asset System | 简称AA)_林新发的博客-CSDN博客
Unity Addressable发布至服务器流程与自动打包代码_凌志·C的博客-CSDN博客
素材网站
阿里巴巴矢量图库,地址:iconfont-阿里巴巴矢量图标库
如何调节你想显示的3d物理模型(比如离得近才显示完全体,离得远显示底模,加上这个组件就可以)
调节阴影距离
Npm相关
npm 安装第三方插件(moment.js)_weixin_44134551的博客-CSDN博客_npm安装moment
https://blog.csdn.net/lenfranky/article/details/90177121
如何方便快速的在指定文件夹打开cmd_LenFranky的博客-CSDN博客_在文件夹打开cmd