Unity3D選擇本地圖片并加載

本文實(shí)例為大家分享了Unity3D選擇本地圖片并加載的具體代碼,供大家參考,具體內(nèi)容如下

成都創(chuàng)新互聯(lián)公司專注于元謀企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。元謀網(wǎng)站建設(shè)公司,為元謀等地區(qū)提供建站服務(wù)。全流程按需策劃設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

①找到System.Windows.Forms.dll:在unity的安裝目錄中找到它,如 

E:\ProgramFiles(x86)\Unity\Editor\Data\Mono\lib\mono\2.0

②設(shè)置.NET 2.0集:Untiy默認(rèn)是.NET 2.0 Subset。在Edit->Project Settings->Player->OtherSettings中修改

③任意打開一項(xiàng)目,新建Plugins文件夾,將找到的System.Windows.Forms.dll復(fù)制進(jìn)去工程文件中(Project)

using UnityEngine;
using System.Collections;
using System;
using System.Windows.Forms;
using System.IO;
using UnityEngine.UI;
public class AddHead : MonoBehaviour {
 private Texture2D img=null;
 public Image image;
 public Sprite sprite;
// Use this for initialization
 void Start () {
 }
 // Update is called once per frame
 void Update () {
 }
 /*void OnGUI(){
 if (GUI.Button (new Rect (0, 0, 100, 20), "選擇文件")) {
 }*/
 public void addhead(){
 OpenFileDialog od=new OpenFileDialog();
 od.Title="請(qǐng)選擇頭像圖片";
 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));
 }
 /*if (img != null) {
 //GUI.DrawTexture(new Rect(0,20,img.width,img.height),img);
 image.sprite=sprite;
 }*/
 }
 IEnumerator GetTexture(string url){
 WWW www = new WWW (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();
 }
 }
}

為按鈕綁定一個(gè)addhead()的方法

Unity3D選擇本地圖片并加載

在編輯器運(yùn)行,當(dāng)出現(xiàn)此彈窗,確定忽略即可。發(fā)布成桌面客戶端是不會(huì)有這個(gè)彈窗的。

Unity3D選擇本地圖片并加載

看運(yùn)行效果

Unity3D選擇本地圖片并加載——》Unity3D選擇本地圖片并加載——》Unity3D選擇本地圖片并加載

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站題目:Unity3D選擇本地圖片并加載
網(wǎng)站鏈接:http://bm7419.com/article0/jddioo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管