文章

【Objective-C】Unity调用iOS分享界面

相关代码如下 +(void)shareMsg:(NSString *)message addUrl:(NSString *)url imgPath:(NSString *)filePath { NSMutableArray *items = [NSMutableArray new];//创建分享内容List [items addObject:message];//添加message到List [items addObject:[NSURL URLWithString:url]];//添加URL到List UIImage *image = [UIImage imageWithContentsOfFile:filePath];//从filePath获取Image if(image != nil)//判断是否存在image [items addObject:image];//添加image到list //初始化UI控制器 UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil]; //为iPad初始化分享界面 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { activity.popoverPresentationController.sourceView = activity.view;//设置目标弹窗 //设置弹窗位置以及大小 activity.popoverPresentationController.sourceRect = CGRectMake( UnityGetGLViewController().view.frame.size.width / 2, UnityGetGLViewController().view.frame.size.height / 2, 1, 1 ); } //显示分享界面 [UnityGetGL

从GitHub直接下载某个文件(转直链)

以这个链接为例 https:// github.com /userName/repoName /blob /branch/file.txt 将 github.com   替换为   raw.githubusercontent.com 去掉 /blob 最终的直链将会是 https:// raw.githubusercontent.com /userName/repoName /branch /file.txt 参考来源 如何直接下载 Github 上的某个文件 | Depp Wang's Blog  

HideFlags在Unity中的应用

HideFlags的作用(来自UnityEngine官方注释): public enum HideFlags { // // Summary: // A normal, visible object. This is the default. None = 0x0, // // Summary: // The object will not appear in the hierarchy. HideInHierarchy = 0x1, // // Summary: // It is not possible to view it in the inspector. HideInInspector = 0x2, // // Summary: // The object will not be saved to the scene in the editor. DontSaveInEditor = 0x4, // // Summary: // The object is not be editable in the inspector. NotEditable = 0x8, // // Summary: // The object will not be saved when building a player. DontSaveInBuild = 0x10, // // Summary: // The object will not be unloaded by Resources.UnloadUnusedAssets. DontUnloadUnusedAsset = 0x20, // // Summary: //

ShaderLab:编译指令

 Unity Shader编译指令 函数命名 顶点着色器(Vertex Shader): #pragma vertex name EX: #pragma vertex vert 片元着色器(Fragment Shader): #pragma fragment name EX: #pragma fragment frag 資料來源:《Unity Shader入门精要》第103頁

ShaderLab:屬性類型及對應Cg變量匹配

 ShaderLab屬性類型: Int 定義語法: number 示例: _Size ( "Size" , Int ) = 10 Float 定義語法: number 示例: _Size ( "Size" ,  Float ) = 10.1 Range(min, max) 定義語法: number 示例: _Size ( "Size" ,  Range(0.0,10.0) ) = 9.1 Color 定義語法: (num,  num,  num,  num ) 示例: _Color ( "Color" ,  Color ) = (1, 1, 1, 1) Vector 定義語法: (num,  num,  num,  num ) 示例: _Position ( "Position" ,  Vector ) = (1, 2, 3, 4) 2D 定義語法: "texture" {} 示例: _MainTex ( "Sprite Texture" ,  2D ) = ""{} Cube 定義語法: "texture" {} 示例: _CubeMap ( "Cube Map" ,  Cube ) =  "white"{} 3D 定義語法: "texture" {} 示例: _3D ( "3D" , 3 D ) =  "black"{} * Unity內置紋理:"white", "black", "gray", "bump" 資料來源:《Unity Shader入门精要》第30頁 属性类型和Cg变量类型匹配关系: 属性:Color, Vector 对应:float4, half4, fixed4 属性:Range, Float 对应:float, half, fixed 属性:2D 对应:sampler2D 属性:Cube 对应:samplerCube 属性:3D 对应:sampler3D 資料來源:《Unity Shade

Unity StreamingAssets文件加載的注意事項

 當我們從StreamingAssets中加載資源時,需要注意的一點是,Windows平台與macOS平台的資源路徑格式並不相同,比如在macOS中使用StreamingAssets路徑時,需要注意在路徑前加入“file://”,否則Unity將不會正常加載資源(Windows平台不需要,但加入 "file://" 後不會影響Windows平台正常訪問該路徑)。 string. Format ( "{0}/{1}" ,   Application . streamingAssetsPath , filePath ) string. Format ( " file:// {0} / {1}" ,  Application . streamingAssetsPath ,  filePath ) OR $ "file://{ Application . streamingAssetsPath }/{ filePath }"  (Unity官方在文檔中並沒有提到這種格式差異。 )

Unity PlayerPrefs 功能拓展

圖片
 Unity版本:2020.3.12f1 Unity自帶的PlayerPrefs默認情況下並不支持本地持久化除String、Int、Float之外的數據類型,如果我們需要通過PlayerPrefs保存其它的任何數據類型,可以通過新建一個自定類並繼承PlayerPrefs來對其進行功能拓展。 這裡就以Bool和Color爲例進行拓展。 這些拓展方法本質上還是基於了它原本自帶String、Int、Float的Get/Set,例如,可以將Bool轉換爲Int整型(false爲0,true爲1)並使用SetInt進行保存。 由於此Class繼承了PlayerPrefs,因此調用SetInt方法時可以省略“PlayerPrefs.” 拓展Bool 拓展Color方法與Bool思路類似,只是將Color轉換成Color的Hex十六進制值並使用SetString進行持久化保存。 拓展Color 使用方法與GetSetFloat/Int/String等自帶方法相同。 示例: 使用方法示例 注意:這裡應當調用PlayerPrefsEX類,而不是自帶的PlayerPrefs。 參考資料 【Unity官方文檔】PlayerPrefs

Unity Mesh Static Batching 網格靜態合批(批處理)

圖片
Unity版本:2018.3.5f1 勾選Auto Combine On Start後此腳本將在初始化(Awake)時將Batch Objects中的Mesh網格進行合併。 腳本程式碼 腳本Inspector 參考資料 【Unity官方文檔】 StaticBatchingUtility .Combine

【Unity編輯器拓展 】拓展編輯器菜單

圖片
在日常開發中我們會經常用到Unity的右鍵菜單,這些菜單都是支持自定義的,我們可以透過編寫自己的 Editor腳本 來向其中添加一些自己的選項。 Unity版本:2020.3.12f1 Project視圖右鍵菜單 Hierarchy視圖右鍵菜單 1. 通過Editor腳本拓展Project視圖右鍵菜單 準備工作: 在Project視圖中創建一個名爲 “Editor” 的文件夾作爲Editor腳本的存放點,還可創建一個名爲 “Scripts” 的文件夾並將“Editor”文件夾移入其中以方便整理。 創建腳本: 在Editor文件夾中創建一個腳本,名稱自定,然後打開。 移除類名後的MonoBehaviour繼承,命名空間引用加入using UnityEngine; 和using UnityEditor; 腳本準備工作 添加如下內容。 創建菜單項 其中的static void的名稱可自定爲其他名稱,[MenuItem]方法用於將這個 static void MenuTool1() 方法添加到右鍵菜單中,第一個參數寫入菜單項的路徑“Assets/Expand Tools/Tool1”,第二個參數( isValidateFunction )填入false如果不需要的話,第三個參數爲該菜單項在菜單中的優先級,數值越小越靠前。 菜單項示例 當我們點擊圖中的 "Tool1" 選項便會調用 static void MenuTool1()  方法 還可以使用相同方法拓展Create菜單 拓展Create菜單 效果預覽 2. 通過Editor腳本拓展Hierarchy視圖右鍵菜單 方法與Project視圖相同,將路徑中的“Assets”改爲“GameObject”即可 拓展Hierarchy視圖右鍵菜單 效果預覽 點擊後會在場景中創建一個球體 參考資料 【Unity官方 文檔 】MenuItem

使用OnGUI實時調試遊戲

圖片
在遊戲開發過程中需要對遊戲進行數值上的調試,可以使用Unity的OnGUI方法來將要調試的數值實時顯示在屏幕上。 不多說,上代碼,這裏以遊戲內實時顯示FPS爲例。 Unity版本:2020.3.12f1 示例代碼 效果示例圖:FPS顯示在左上角 參考資料 【Unity官方文檔】OnGUI 【Unity官方文檔】GUI 【Unity官方文檔】UnityEngine.IMGUIModule

C# 隱式轉換

圖片
有些時候我們需要將兩個不同的類相互進行轉換,就像Object類可以轉換爲Bool一樣(如圖1),不論是使用 if(Object) 還是 if(Object != null) 效果完全一致,這實際上是因爲Unity在Object類中已經爲我們定義了Object到Bool的隱式轉換(Implicit Conversion,如圖2),同樣的,我 們也可以使用相同的方式讓一組自己的自定類之間可以相互轉換。 Unity版本:2020.3.12f1 圖1:Object隱式轉換爲Bool 圖2:UnityEngine.Object類中的隱式轉換 首先,我们分別定義兩個名爲"Class1""Class2"的類(如圖3)。 圖3:定義類 定義Class1到Class2以及Class2到Class1的隱式操作符 (如圖4) 。 圖4:定義兩個類之間的隱式轉換關係 經過一系列的操作,現在可以發現Class1和Class2之間可以相互轉換了(如圖5) 圖5:class1和class2相互轉換 注意: implicit operator只能在要轉換的Class中的其中一個中定義,不可在除這些類之外的任何一個類中定義(包括父類)。 錯誤示例 正確示例 同時,同一個 implicit operator在兩個類中只能存在一個