Unity3D游戏开发学习园地 http://blog.okbase.net/unity3d 解决打包AssetBundle时Shader(材质)丢失的问题 http://blog.okbase.net/unity3d/archive/56748.html unity3d 2018/9/21 8:10:17 菜单Edit->Project Settings->Graphics

打开Graphics Settings选项窗口,
把shader添加到Always Included Shaders,可以修改Size以增加shader数量。

 

]]>
Unity3D场景切换时传递数据 http://blog.okbase.net/unity3d/archive/56743.html unity3d 2018/9/15 22:33:42 LoadLevelAsync切换场景,如何将Scene1的数据传递到Scene2:

 

Scene1里面:
PlayerPrefs.SetString("name","okbase");
PlayerPrefs.SetInt("age",25);
PlayerPrefs.SetFloat("work",3.6f);


Scene2里面:
string name = PlayerPrefs.GetString("name");
int age = PlayerPrefs.GetInt("age");
float work = PlayerPrefs.GetFloat("work");

]]>
Unity3D动态加载Assets里的Material http://blog.okbase.net/unity3d/archive/56737.html unity3d 2018/9/4 22:49:03 首先,material需要放在Resources目录下,例如目录结构是这样的:

Assets/Resources/Materials/test.mat

 

加载代码如下:
Material mt = Resources.Load<Material>("Materials/test");

]]>
AVPro Video无法播放视频的问题 http://blog.okbase.net/unity3d/archive/56734.html unity3d 2018/8/29 17:52:17 AVPro Video是一款非常好用的Unity3D视频播放插件,但某些格式的视频无法播放,

安卓播放器有传统的mediaplayer,和一种新的exoplayer,我们将该插件设置为exo player模式就可以播放一些新的格式的视频。

]]>
Unity3D跨场景的背景音乐 http://blog.okbase.net/unity3d/archive/56690.html unity3d 2018/7/14 18:41:32 步骤如下:

1、建立一个空的GameObject,命名为BackgroundMusic
2、将音乐文件添加到BackgroundMusic这个游戏对象中
3、添加Audio Listener组件到BackgroundMusic中
4、创建脚本BackgroundMusic.cs,代码如下:

public class BackgroundMusic : MonoBehaviour {

	static BackgroundMusic _instance;

	// Use this for initialization
	void Start () {

	}

	public static BackgroundMusic instance    {
		get
		{
			if (_instance == null)
			{
				_instance = FindObjectOfType<BackgroundMusic>();
				DontDestroyOnLoad(_instance.gameObject);
			}
			return _instance;
		}
	}

	void Awake()
	{
		//不销毁,若存在重复则销毁
		if (_instance == null)
		{
			_instance = this;
			DontDestroyOnLoad(this);
		}
		else if (this != _instance)
		{
			Destroy(gameObject);
		}
	}

	
	// Update is called once per frame
	void Update () {
		
	}
}

将该脚本添加到BackgroundMusic游戏对象中。

]]>
Unity3d生成安卓APK时遇到的两个错误 http://blog.okbase.net/unity3d/archive/56671.html unity3d 2018/6/23 16:10:35 导入某个插件后就发生了无法编译的问题。

错误一:

Error: [Temp\StagingArea\AndroidManifest-main.xml:2, E:\Unity3DProjects\vr360\Temp\StagingArea\android-libraries\vrlib\AndroidManifest.xml:3] Main manifest has <uses-sdk android:minSdkVersion='16'> but library uses minSdkVersion='19'

 

解决:

这是最低sdk版本要求不一致导致的,修改Assets/Plugins/Android/AndroidManifest.xml,将minSdkVersion修改为19即可:

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="22" />

 

错误二:
CommandInvokationFailure: Unable to merge android manifests. See the Console for more details.
D:\Program Files\Java\jdk1.8.0_121\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="D:/AndroidSDK\tools" -Dfile.encoding=UTF8 -jar "E:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\sdktools.jar" -

stderr[

]
stdout[
Warning: [Temp\StagingArea\AndroidManifest-main.xml:1, E:\Unity3DProjects\vr360\Temp\StagingArea\android-libraries\vrlib\AndroidManifest.xml:8] Main manifest has <uses-feature android:glEsVersion='0x00010000'> but library uses glEsVersion='0x00030000'
Note: main manifest lacks a <uses-feature android:glEsVersion> declaration, and thus defaults to glEsVersion=0x00010000.
]
exit code: 1
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidSDKTools.RunCommandInternal (System.String javaExe, System.String sdkToolsDir, System.String[] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidSDKTools.RunCommandSafe (System.String javaExe, System.String sdkToolsDir, System.String[] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.HostView:OnGUI()

 

解决:

这是opengl版本不一致导致的,修改Assets/Plugins/Android/AndroidManifest.xml,修改glEsVersion版本即可,

<uses-feature android:glEsVersion="0x00030000" android:required="true" />

]]>
小米VR眼镜一体机开发初体验 http://blog.okbase.net/unity3d/archive/56670.html unity3d 2018/6/20 21:33:34 1)打开调试模式

安装好小米VR app后,点“我的”,“设置”,“关于帮助”,点5下LOGO,即可打开调试模式。

 

2)安装apk
adb install -r "e:\temp\VR\test.apk"

 

3)运行apk
在界面中会有一个apk图标,点击即可运行。
或者用命令行:adb shell am start -n 你的APP包名/com.mi.dlabs.vr.sdk.plugins.unity.MiVRUnityPlayerActivity

 

4)闪退的解决
运行后发现闪退问题,于是打印日志:
4.1)adb logcat -c 清空日志历史记录
4.2)重新运行app
4.3)adb logcat -d 打印日志

 

Caused by: java.lang.SecurityException: Permission Denied
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1684)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1637)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at com.mi.dlabs.vr.IVRService$Stub$a.relFreq(SourceFile:419)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at com.mi.dlabs.vr.sdk.VrServiceClient.relFreq(SourceFile:172)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at com.mi.dlabs.vr.sdk.VrServiceClient$1.onServiceConnected(SourceFile:68)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1453)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1481)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:751)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6121)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
06-20 19:38:18.643 3176 3176 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

 

发现是全新问题,仔细阅读了开发文档,发现没有放置开发者签名文件。
用 adb devices 获取序列号
访问http://dev.xiaomi.com/vr/signature/index 生成签名文件放在 Project/Assets/Plugins/Android/assets/MiVR_sig/
重新Build即可。

本文网址:http://blog.okbase.net/unity3d/archive/56670.html

 

5)usb存储器消失的问题

驱动程序正确,无法显示USB存储器,插拔USB线还是这样。遇到这种情况,请按照第1)的方法进行开发模式的切换。

 

]]>
Unity3D绑定按钮Click事件 http://blog.okbase.net/unity3d/archive/56667.html unity3d 2018/6/12 13:44:04 方式一:界面中绑定

脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonClick : MonoBehaviour {
  public void Click(){
      Debug.Log ("Button Clicked.");
  }
}

在Hierarchy里添加按钮UI->Button,命名为:PlayButton
选中PlayButton,Add componets 添加脚本ButtonClick
On Click(),选择"+" add to list,Select Object选择PlayButton(因为脚本关联到了PlayButton,所以选择PlayButton,必须一致,否则会无法列出function),然后在下拉列表框里选择ButtonClick.Click

 

方式二:代码绑定
using UnityEngine.Events;

public class ClickObject2 : MonoBehaviour
{
    void Start ()
    {
        //获取按钮游戏对象
        GameObject btnObj = GameObject.Find ("Canvas/Button");
        //获取按钮脚本组件
        Button btn = (Button) btnObj.GetComponent<Button>();
        //添加点击侦听
        btn.onClick.AddListener (delegate() {
            onClick(btnObj);
        });
    }

    void onClick (GameObject obj)
    {
        Debug.Log ("click: " + obj.name);
    }
}

]]>
Unity3d安卓使用陀螺仪旋转画面 http://blog.okbase.net/unity3d/archive/56666.html unity3d 2018/6/10 23:12:02 代码如下

protected void Start()
{
    if (Application.platform == RuntimePlatform.Android) {
        //设置设备陀螺仪的开启/关闭状态,使用陀螺仪功能必须设置为 true
        Input.gyro.enabled = true;
        //设置陀螺仪的更新检索时间,即隔 0.1秒更新一次
        Input.gyro.updateInterval = 0.1f;
    }
}

 

protected void Update()
{
    this.transform.localRotation = CameraRotation();
}

 

Quaternion CameraRotation()
{
    Quaternion input = Input.gyro.attitude;
    input = Quaternion.Euler(90, 0, 0) * (new Quaternion(-input.x, -input.y, input.z, input.w));
    return input;
}

]]>
Unity3D采用全景图创建天空盒Skybox http://blog.okbase.net/unity3d/archive/56665.html unity3d 2018/6/9 21:25:20 1)导入全景大图

Texture Type:Default
Texture Shape:Cubemap
Mapping:Latitude-Longitude Layout
FilterMode:Bilinear

 

2)新建Material材质
Create->Material

Shader选择Skybox/Cubemap
Cubemap (HDR) 选择cubemap文件,就是刚才导入的全景大图

 

3.1)方式一:添加skybox组件
切换到Camera,添加skybox组件,Custom Skybox 选择刚才建好的material

 

3.2)方式二:创建Sphere

Create-> 3D Object ->Sphere

Mesh Renderer中Element 0选择刚才建好的material

Scale要设置大一些,例如:50


参考文章:https://www.cnblogs.com/zhaoqingqing/p/5537961.html

]]>