歡迎加入Unity業內qq交流羣:956187480
qq掃描二維碼加羣
最近給公司寫了一個自定義海報生成工具,主要功能如下
1.數據信息走csv配置讀取
表的配置讀取解析參考之前博文 https://blog.csdn.net/qq_37310110/article/details/89488295
2.自動化生成節日路徑
IO系統的學習及應用參考之前博文 https://blog.csdn.net/qq_37310110/article/details/87880438
3.界面元素的自定義位置拖拽
ui元素拖拽分兩種情況,具體如何設置是根據Canvas的模式來說的
第一種:如果Canvas的模式爲OverLay
public class DragUI : MonoBehaviour, IDragHandler, IPointerDownHandler
{
private Vector2 offsetPos; //臨時記錄點擊點與UI的相對位置
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position - offsetPos;
}
public void OnPointerDown(PointerEventData eventData)
{
offsetPos = eventData.position - (Vector2)transform.position;
}
}
轉存失敗重新上傳取消轉存失敗重新上傳取消轉存失敗重新上傳取消第二種:如果Canvas的模式爲Camera
public class DragUI : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
Vector2 position;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.gameObject.GetComponent<RectTransform>(), eventData.position, Camera.main, out position))
{
return;
}
transform.localPosition = position;
}
}
轉存失敗重新上傳取消轉存失敗重新上傳取消轉存失敗重新上傳取消4.海報背景自助設定
這裏用到了一個程序集:System.Windows.Forms,
在unity的安裝路徑:D:\Package\2018.4.0f1\Unity\Editor\Data\Mono\lib\mono\2.0,導入Plugins文件夾即可
public void SelectMainBg()
{
try
{
OpenFileDialog od = new OpenFileDialog();
od.Title = "請選擇海報背景";
od.Multiselect = false;
od.Filter = "圖片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
if (od.ShowDialog() == DialogResult.OK)
{
//Debug.Log(od.FileName);
StartCoroutine(GetTexture("file://" + od.FileName, mainBg));
}
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
IEnumerator GetTexture(string url,Image image)
{
WWW www = new WWW(url);
Debug.Log(url);
yield return www;
if (www.isDone && www.error == null)
{
img = www.texture;
sprite = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite;
Debug.Log(img.width + " " + img.height);
byte[] date = img.EncodeToPNG();
}
else
{
Debug.Log(www.error);
}
}
5.文字顏色大小可通過色板設置
這個功能本來想自己寫一個獲取色環的工具出來的,後來因爲時間上的原因就用了網上現有的色板工具很方便,再二次開發一下加入了字體大小的設置下載路徑:https://download.csdn.net/download/qq_37310110/12311575
6.截圖保存自定義路徑
截圖的話 就直接用的unity自帶的截圖工具
IEnumerator SaveImage(string path)
{
ScreenCapture.CaptureScreenshot(path, 5);
yield return new WaitUntil(() => true);
//yield return new WaitForSeconds(1);
Debug.Log(path.Split('/')[5]+"--海報生成成功!!");
}
歡迎加入Unity業內qq交流羣:956187480
qq掃描二維碼加羣