void Update () {
//判断异步对象并且异步对象没有加载完毕,显示进度
if(getSubjectData!=null&&!getSubjectData.isDone)
{
Debug.LogError("正在加载...");
}
}
1.Unity3d数据的持久化。切换场景时,或下次打开程序时可用。
PlayerPrefs.SetString("Name", "Jams");
string name= PlayerPrefs.GetString("Name");
2.Unity3d加载资源。
(1)加载某文件下某类资源Object[] m_skyboxs = Resources.LoadAll("skybox", typeof(Material));
for(int i = 0; i < m_skyboxs.Length; i++)
{
Material mat = (Material)m_skyboxs[i];
//使用mat
}
(2)加载某一个资源
Material mat = (Material)Resources.Load("skybox/school3.mat");
3.更改天空盒子
Material mat = (Material)Resources.Load("skybox/school3.mat");
Skybox sb = currentCamera.GetComponent();
sb.material = mat;
4.需要在某个物体上存储变量,如人Person的姓名、年龄、性别
在Person上添加组件PersonInfoScript,并添加变量Name、Old、 Sex,这样就可存储了。
public class PersonInfoScript : MonoBehaviour {
//音乐文件
public AudioSource ButtonVoice;
[HideInInspector]//不显示在Inspector面板中
public string Name
[HideInInspector]
public string Old;
[HideInInspector]
public string Sex;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//....
}
使用方法:
personObj.GetComponent().Name = "Jams";
5.将Texture2D转化为Sprite
Sprite tmpsprite = Sprite.Create(www.texture,new Rect(0,0,www.texture.width, www.texture.height),new Vector2(0.5f,0.5f));
6.post请求
WWWForm form = new WWWForm();
form.AddField("userid","ABC");
WWW getRecommandedData = new WWW("http://localhost/test.php",form);
yield return getRecommandedData;
if(!string.IsNullOrEmpty(getRecommandedData.error))
{
Debug.LogError("无法连接到服务器,请刷新重试");
yield break;
}
string srcString = getRecommandedData.text;
//如果要显示加载进度在Update()函数中更新
void Update () {
//判断异步对象并且异步对象没有加载完毕,显示进度
if(getSubjectData!=null&&!getSubjectData.isDone)
{
Debug.LogError("正在加载...");
}
}
7.给按钮加声效,当鼠标滑过按钮时滴一声。给按钮加一个Event Trigger组件,在组件中加一个Pointer Enter事件,在这个事件中播放声音就要以了。
8.下载文件
WWW downloadOperation = new WWW(url);
yield return downloadOperation;
if(string.IsNullOrEmpty(downloadOperation.error))
{
Byte[] b = downloadOperation.bytes;
File.WriteAllBytes(localPath, b);
}
9.全景图片的显示,建立一个内侧球体(球体法线向内,否则会看不见球内部),将相机放入球内就可以看球内壁了,将要显示的全景图片作为材质附加到球体上就可以了。如果要切换图片,修改材质Material的纹理就可以了。
WWW www = new WWW("file:///C:/test.png");
yield return www;
if(string.IsNullOrEmpty(www.error))
{
Material mat = (Material)Resources.Load("panorama", typeof(Material));
mat.mainTexture = www.texture;
}
10.全景视频的播放。使用AVProVideo组件中的预制件360SphereVideo.prefab或360CubeVideo.prefab就可实现全景视频播放。如要切换视频,修改
MediaPlayer组件的File属性就可以了,也可以OpenVideoFromFile方法。
MediaPlayer mp = gameObject.GetComponentInChildren();
mp.OpenVideoFromFile(RenderHeads.Media.AVProVideo.MediaPlayer.FileLocation.RelativeToPeristentDataFolder, "test.mp4",true);
11.安卓环境下文件下载保存目录为Application.persistentDataPath
12.加掉大图片到纹理
WWW www = new WWW("file:///C:/test.jpg");
yield return www;
if(string.IsNullOrEmpty(www.error)&&www.isDone)
{
Texture2D txt2d = new Texture2D(2000, 1000, TextureFormat.RGB24, true);//经测试,最后一个参数一定要为true
www.LoadImageIntoTexture(txt2d);
Material mat = (Material)Resources.Load("panorama", typeof(Material));
if(mat != null)
mat.mainTexture = txt2d;
}