【Unity3D日常開發(fā)】(十三)Unity3D中實現(xiàn)熱力圖、風(fēng)向圖、溫度圖效果
推薦閱讀
CSDN主頁
GitHub開源地址
Unity3D插件分享
簡書地址
我的個人博客
QQ群:1040082875
大家好,我是佛系工程師
☆恬靜的小魔龍☆
,不定時更新Unity開發(fā)技巧,覺得有用記得一鍵三連哦。
一、前言
因一個任務(wù)要完成如何在Unity上面實現(xiàn)熱力圖的效果,所以百度了很久,發(fā)現(xiàn)資料很少,現(xiàn)在就把我總結(jié)的如何在Unity上面基于Canvas實現(xiàn)熱力圖效果的實現(xiàn)過程分享出來, 此前轉(zhuǎn)載了一篇主要講的是如何根據(jù)數(shù)據(jù)值,在Canvas上重新繪制RGBA的值,完成熱力圖的繪制,不過用的是H5寫的,我修改了一下,用C#重寫的
效果圖:
項目源文件已經(jīng)上傳CSDN:
https://download.csdn.net/download/q764424567/13109963
二、參考資料
1.基于Canvas的熱力圖繪制方法【http://www.blueidea.com/tech/web/2010/7933.asp 】
2.Unity(OpenGL)實現(xiàn)“陰陽師畫符”、劃線功能【https://blog.csdn.net/yuanhandsome/article/details/78366250】
三、正文
實現(xiàn)過程:
1.首先從文檔中解析數(shù)據(jù),獲取需要的數(shù)據(jù)(當(dāng)然,這一步也可以在網(wǎng)上獲取數(shù)據(jù),然后再解析)
2.將獲取的數(shù)據(jù)進行分析
3.保存貼圖到Texture,將保存的貼圖賦值給Image的Sprite(保存的貼圖也可以賦值給物理對象的貼圖)
4.清除數(shù)據(jù)
第一步、解析數(shù)據(jù)
就以下面這一組數(shù)據(jù)為例
第一行數(shù)據(jù),代表是的模擬網(wǎng)格 160160
第二行數(shù)據(jù),模擬半徑 -80km至80km范圍;東西方向(x軸)區(qū)間范圍,西→東
第三行數(shù)據(jù),劑量值(z軸),即160160網(wǎng)格范圍內(nèi)的最小值和最大值
第6-164,表示各個網(wǎng)格點的濃度值
就是說有160*160個數(shù)據(jù),可以用二維數(shù)組去接收數(shù)據(jù)
然后用一個List數(shù)組去接收解析完的數(shù)據(jù)
using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; using UnityEngine.EventSystems; public class Draw : MonoBehaviour { //文件路徑名 private string fileName; //讀取數(shù)據(jù) private string str; //保存數(shù)據(jù) private List
第二步、分析數(shù)據(jù)
數(shù)據(jù)保存到m_Sum二維數(shù)組中,剩下的就是從二維數(shù)組中讀取數(shù)據(jù),然后進行分析
讀取到的數(shù)據(jù)是最后兩位的數(shù)據(jù),然后將數(shù)據(jù)轉(zhuǎn)成int類型進行對比
實現(xiàn)代碼
//對比數(shù)據(jù) public void DataCompare() { //因為二維數(shù)組的數(shù)量是159*159所以就在這個范圍內(nèi)讀取 for (int i = 0; i < 159; i++) { for (int j = 0; j < 159; j++) { //數(shù)據(jù)對比 int temp = m_Sum[i, j]; if (temp <= 6) { Debug.Log("透明"); } else if (temp > 6 && temp <= 7) { Debug.Log("紅色"); } else if (temp > 7 && temp <= 8) { Debug.Log("橘色"); } else if (temp > 8 && temp <= 9) { Debug.Log("黃色"); } else if (temp > 9 && temp <= 10) { Debug.Log("綠色"); } else if (temp > 10 && temp <= 11) { Debug.Log("藍色"); } else if (temp > 11 && temp <= 12) { Debug.Log("靛藍色"); } else if (temp > 12) { Debug.Log("紫色"); } } } }
第三步、保存貼圖
最重要的一步,如何將分析過的數(shù)據(jù)生成貼圖,并且賦值給Image呢
主要就是這兩行的代碼
//保存貼圖變量 private Texture2D texture; private Image targetMaterial; //創(chuàng)建設(shè)置貼圖文件 void CreatTexture() { texture = new Texture2D(m_Width, m_Height); int targetWidth = m_Width; int targetHeight = m_Height; for (int i = 0; i < targetWidth; i++) { for (int j = 0; j < targetHeight; j++) { int temp = m_Sum[i, j]; if (temp <= 6) { texture.SetPixel(i, j, m_ColorTransparency); } else if (temp > 6 && temp <= 7) { texture.SetPixel(i, j, m_ColorRed); } else if (temp > 7 && temp <= 8) { texture.SetPixel(i, j, m_ColorOrange); } else if (temp > 8 && temp <= 9) { texture.SetPixel(i, j, m_ColorYellow); } else if (temp > 9 && temp <= 10) { texture.SetPixel(i, j, m_ColorGreen); } else if (temp > 10 && temp <= 11) { texture.SetPixel(i, j, m_ColorBlue); } else if (temp > 11 && temp <= 12) { texture.SetPixel(i, j, m_ColorIndigo); } else if (temp > 12) { texture.SetPixel(i, j, m_ColorPurple); } } } //應(yīng)用貼圖 texture.Apply(); //將貼圖數(shù)據(jù)賦值給Image的sprite targetMaterial.sprite = Sprite.Create(texture, new Rect(0, 0, m_Width, m_Height), Vector2.zero); ; }
第四步、清除數(shù)據(jù)
清除數(shù)組中的數(shù)據(jù),供下次讀取數(shù)據(jù)
//清理數(shù)組中的數(shù)據(jù) public void ClearList() { numberList.Clear(); }
整體代碼
using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; using UnityEngine.EventSystems; public class Draw : MonoBehaviour { //顏色設(shè)置 public Color m_ColorTransparency; public Color m_ColorRed; public Color m_ColorOrange; public Color m_ColorYellow; public Color m_ColorGreen; public Color m_ColorBlue; public Color m_ColorIndigo; public Color m_ColorPurple; //保存貼圖 private Texture2D texture; private Image targetMaterial; //文件路徑名 private string fileName; //讀取數(shù)據(jù) private string str; //保存數(shù)據(jù) private List
四、工程文件
鏈接:https://pan.baidu.com/s/1akdyDVrwHjQJm9RvD4Rh8w
提取碼:y4be
Github地址:
https://github.com/764424567/Demo_DrawImg
Gitee地址(嫌棄Github地址下載太慢可以用碼云):
https://gitee.com/itMonon/Demo_DrawImg
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。