Unity3D游戏开发学习园地 http://blog.okbase.net/unity3d DoTween移动物体到指定地点 http://blog.okbase.net/unity3d/archive/56431.html unity3d 2020-3-22 21:28:00 1)可以用DOTween.To

例如

Transform cube = GameObject.Find("Cube").transform;
DOTween.To(() => cube.position, x => cube.position = x, new Vector3(10, 10, 10), 1.5f);

2)可以用DOMove

例如

Transform cube = GameObject.Find("Cube").transform;
cube.transform.DOMove(new Vector3(10, 10, 10), 1.5f);

SetEase可以设置运动的方式

例如完成匀速运动

Tweener tweener = cube.transform.DOMove(new Vector3(10, 10, 10), 1);
tweener.SetEase(Ease.Linear);
tweener.onComplete = delegate () {
    Debug.Log("移动完毕事件");
};
]]>
Action委托的几种形式 http://blog.okbase.net/unity3d/archive/56430.html unity3d 2020-3-22 21:18:00 记录一下以作备忘:

void Start()
{
	System.Action OnCallback1 = delegate ()
	{
		Debug.Log("回调1");
	};

	Test(OnCallback1);
	Test(OnCallback2);
}

void OnCallback2(int a, float b)
{
	Debug.Log("回调2:" + a + "," + b);
}

void Test(System.Action oncomplete1)
{
	oncomplete1?.Invoke();
}

void Test(System.Action<int, float> oncomplete2)
{
	oncomplete2?.Invoke(100, 12.5f);
}
]]>
Pico VR一体机如何开启USB调试模式 http://blog.okbase.net/unity3d/archive/56422.html unity3d 2020-3-8 17:54:00 在主界面进入 VR 设置(点顶部的设置按钮),进入“关于”,看到 PUI 版本信息,连续点击版本号 4次以上,点击后右边出现“高级设置”。

 

进入“高级设置”,往上拖动页面,进入“系统”选项,进入“开发者选项”,往上拖动页面,找到“USB调试”并启用。
如果该选项本来就是启用的,可以先禁用再启用。有时候USB调试失效,我们运行ADB命令的时候会出现找不到设备错误:


error: no devices/emulators found
adb: error: failed to get feature set: no devices/emulators found
- waiting for device -

 

我们需要先禁用“USB调试”再重新启用。

]]>
Unity项目切换到Anroid平台后Build按钮灰色禁用 http://blog.okbase.net/unity3d/archive/56421.html unity3d 2020-3-7 22:46:00 解决方法:菜单 Edit/Project Settings/Player,Other Settings->Color Space需要切换到Gamma,Build按钮会恢复有效。

]]>
Unity3D第一人称视角MouseLook鼠标控制摄像机上下左右转动 http://blog.okbase.net/unity3d/archive/56420.html unity3d 2020-3-6 23:35:00 MainCamera添加First Person Controller组件后,鼠标只能控制镜头上下动,不能左右转动。稍微修改一下MouseLook实现上下左右都能动。代码如下:

 

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Characters.FirstPerson
{
    [Serializable]
    public class MouseLook
    {
        public float XSensitivity = 2f;
        public float YSensitivity = 2f;
        public bool clampVerticalRotation = true;
        public float MinimumX = -90F;
        public float MaximumX = 90F;
        public bool smooth;
        public float smoothTime = 5f;
        public bool lockCursor = true;


        private Quaternion m_CharacterTargetRot;
        private Quaternion m_CameraTargetRot;
        private bool m_cursorIsLocked = true;

        public void Init(Transform character, Transform camera)
        {
            m_CharacterTargetRot = character.localRotation;
            m_CameraTargetRot = camera.localRotation;
        }


        public void LookRotation(Transform character, Transform camera)
        {
            float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
            float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;

            camera.Rotate(Vector3.up, yRot, Space.World);
            camera.Rotate(Vector3.right, -xRot, Space.Self);

            /*
            m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
            m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
            Debug.Log("x:" + xRot.ToString()); Debug.Log("y:" + yRot.ToString());
            if (clampVerticalRotation)
                m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);

            if(smooth)
            {
                character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
                    smoothTime * Time.deltaTime);
                camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
                    smoothTime * Time.deltaTime);
            }
            else
            {
                character.localRotation = m_CharacterTargetRot;
                camera.localRotation = m_CameraTargetRot;
            }
            */
            UpdateCursorLock();
        }

        public void SetCursorLock(bool value)
        {
            lockCursor = value;
            if(!lockCursor)
            {//we force unlock the cursor if the user disable the cursor locking helper
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
            }
        }

        public void UpdateCursorLock()
        {
            //if the user set "lockCursor" we check & properly lock the cursos
            if (lockCursor)
                InternalLockUpdate();
        }

        private void InternalLockUpdate()
        {
            if(Input.GetKeyUp(KeyCode.Escape))
            {
                m_cursorIsLocked = false;
            }
            else if(Input.GetMouseButtonUp(0))
            {
                m_cursorIsLocked = true;
            }

            if (m_cursorIsLocked)
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
            else if (!m_cursorIsLocked)
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
            }
        }

        Quaternion ClampRotationAroundXAxis(Quaternion q)
        {
            q.x /= q.w;
            q.y /= q.w;
            q.z /= q.w;
            q.w = 1.0f;

            float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);

            angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);

            q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);

            return q;
        }

    }
}
]]>
用adb命令运行远程调试工具Unity Remote http://blog.okbase.net/unity3d/archive/56350.html unity3d 2019-10-14 17:48:00 1)安装com.unity3d.genericremote_2.0.apk

adb install -r "e:\temp\com.unity3d.genericremote_2.0.apk"

 

2)命令行启动

adb shell am start -n com.unity3d.genericremote/com.unity3d.player.UnityPlayerActivity

 

]]>
Unity3D未能找到类型或命名空间名“HttpClient” http://blog.okbase.net/unity3d/archive/56344.html unity3d 2019-8-28 22:58:00 Assets/UnityGLTF/Scripts/Loader/WebRequestLoader.cs(24,20): error CS1070: The type `System.Net.Http.HttpClient' has been forwarded to an assembly that is not referenced. Consider adding a reference to assembly `System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

错误 CS0246 未能找到类型或命名空间名“HttpClient”(是否缺少 using 指令或程序集引用?)


右键点解决方案,管理解决方案的NuGet程序包,浏览System.Net.Http,勾选项目,安装。

E:\Unity3DProjects\test\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll

 

貌似可以了,但重启Unity编辑器后又不行了。

 

尝试方法:

在Assets目录下建立名为mcs.rsp的文件,内容只有一行:

-r:System.Net.Http.dll

 

重启Unity编辑器,问题依旧存在。

 

解决:这个问题在Unity3D 2018.3以上版本已解决,只能用新版本了。

 

]]>
缺少GVRViewerMain的错误 http://blog.okbase.net/unity3d/archive/56286.html unity3d 2019-4-19 10:32:00 【译文】

缺少GVRViewerMain

 

Unity 5.6及更高版本中已经对Daydream/Cardboard提供原生支持,不再需要GVRViewerMain,只需在Player Settings中简单地设置VR SDK,Player Settings > Other Settings > Virtual Reality SDKs:根据需要添加Daydream或Cardboard。 您的主摄像头将自动成为VR摄像头。 GVR Unity SDK(自v1.40起)不再支持早于5.6的Unity版本。

 

【原文】

GVRViewerMain is missing

 

With the native support for Daydream/Cardboard in Unity 5.6 and beyond, there is no longer a need for GVRViewerMain, you simply configure the VR SDK in Player Settings > Other Settings > Virtual Reality SDKs: add Daydream and/or Cardboard as appropriate for your project. Your main camera will automatically become a VR camera. The GVR Unity SDK (since v1.40) no longer supports versions of Unity older than 5.6.

]]>
XLua错误一例:LuaException: [string "MyLua"]:1: attempt to call a nil value http://blog.okbase.net/unity3d/archive/56244.html unity3d 2019-3-23 10:49:00 错误信息:

LuaException: [string "MyLua"]:1: attempt to call a nil value (method 'Lua_MyMethod')
stack traceback:
[string "MyLua"]:1: in main chunk
XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) (at Assets/XLua/Src/LuaEnv.cs:440)
XLua.LuaEnv.DoString (System.Byte[] chunk, System.String chunkName, XLua.LuaTable env) (at Assets/XLua/Src/LuaEnv.cs:273)
XLua.LuaEnv.DoString (System.String chunk, System.String chunkName, XLua.LuaTable env) (at Assets/XLua/Src/LuaEnv.cs:287)

 

解决方法:

Unity3D集成环境菜单XLua->Clear Generated Code, Generate Code 重新生成代码。

]]>
使用UniFBX在运行时导入FBX网格 http://blog.okbase.net/unity3d/archive/56210.html unity3d 2019-3-11 16:32:00 入门

要想正确地使用该asset,请按下面步骤操作:


1)在层次结构选项卡中建立一个空的GameObject并重命名(例如UniFBX)


2) 添加位于CSEU/UniFBX/Lib中的名为"UniFBXImporter.cs"脚本到UniFBX游戏对象
注意:如果你的平台是Standalone或Android,另外一个名为"UniFBXThread.cs"会被添加到新建的游戏对象中。该组件用于异步加载文件。


3) 在UniFBX游戏对象组件url字段中设置路径,例如:
https://c8d209f2f8b575dc564af150673baa682b230943.googledrive.com/host/0B4yeGzF6xDFuc2RJeW5HZ0ZhcjQ/models/
在Texture Folder字段中添加
https://c8d209f2f8b575dc564af150673baa682b230943.googledrive.com/host/0B4yeGzF6xDFuc2RJeW5HZ0ZhcjQ/models/tex/
(jpg,png纹理必须保存在此文件夹路径中)
在Filename(FBX)字段中添加“modelname”。不要加上扩展名“.fbx”
由于url字段是Google云盘中的网址,因此您可以尝试使用某些model名称,例如:
Cube001, cube002, cube003, cube004, palm, landscape, rabbit, apartament, levelGeometry
某些model包含在Tutoriral/0000/Models/中的包中

可以在3DMax或Maya中,在Exporter Editor中选择,所有模型导出为FBX ASCII格式。
FBX File Format选择Type:ASCII,Version:FBX 2012,2013,2014,2015版本。
勾选:Triangulate

 

4) 添加一个调用导入功能的简单脚本。您可以为创建的新的游戏对象添加新脚本,并添加以下行:


脚本可以在"/CSEU/UniFBX/Scripts/"中找到。

 

游戏对象必须如上图所示。但这个小例子可以在位于"/CSEU/UniFBX/Scenes/"的UniFBX.unity场景中找到它。
您可以打开位于/CSUE/UniFBX/Tutorial/0000/Scene/ UniFBX-Tutorial-0000.unity的场景进行测试。

 

]]>