Compare commits
7 Commits
59d430a388
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c34aac38ab | |||
| 92c8721d7a | |||
| 917ba0626a | |||
| 135ac2c03c | |||
| a47d27059d | |||
| c87e4d9ddf | |||
| 83cb8eaaea |
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: StudyCase1
|
||||
date: 2025-11-26 15:35
|
||||
date: 2025-11-27 16:35:00
|
||||
tags: [studycase, unity]
|
||||
pinned: true
|
||||
head:
|
||||
@@ -76,7 +76,7 @@ head:
|
||||
|
||||
<img src="/image/studycase1/搜索所有材质.png" data-fancybox="gallery"/>
|
||||
|
||||
- 依次点击,切换到URP
|
||||
- 全选材质,点击Edit>Rendering>Materials>Convert Selected Built-in Materials to URP
|
||||
|
||||
<img src="/image/studycase1/切换到URP.png" data-fancybox="gallery"/>
|
||||
|
||||
@@ -85,12 +85,12 @@ head:
|
||||
<img src="/image/studycase1/点击确定.png" data-fancybox="gallery"/>
|
||||
|
||||
## 5.勇者行迹录-章节任务
|
||||
### 添加虚拟相机根节点
|
||||
### 5.1 添加虚拟相机根节点
|
||||
- 在主相机`Main Camera`上添加 `CinemachineBrain`作为根节点以便统一控制
|
||||
|
||||
<img src="/image/studycase1/添加虚拟相机根节点.png" data-fancybox="gallery"/>
|
||||
|
||||
### 创建玩家节点
|
||||
### 5.2 创建玩家节点
|
||||
- 创建 `Player` 空节点,创建`Forward`和`Model`空节点并作为`Player`子节点以及虚拟相机
|
||||
- 注意`Forward`和`Model`的尽量不要有偏移
|
||||
|
||||
@@ -100,12 +100,12 @@ head:
|
||||
|
||||
<img src="/image/studycase1/玩家节点.png" data-fancybox="gallery"/>
|
||||
|
||||
### 配置虚拟相机
|
||||
### 5.3 配置虚拟相机
|
||||
- 将虚拟相机 `Follow`、`LookAt` 指向玩家模型,调整 Body/ Aim,使镜头保持第三人称视角
|
||||
|
||||
<img src="/image/studycase1/配置虚拟相机.png" data-fancybox="gallery"/>
|
||||
|
||||
### 创建 InputActions
|
||||
### 5.4 创建 InputActions
|
||||
- 创建`Resources`文件夹,右键 `Create > Input Actions`,命名为 `PlayerInputActions`
|
||||
|
||||
<img src="/image/studycase1/创建InputActions.png" data-fancybox="gallery"/>
|
||||
@@ -124,7 +124,7 @@ head:
|
||||
|
||||
<img src="/image/studycase1/jump绑定space.png" data-fancybox="gallery"/>
|
||||
|
||||
### 编写脚本`ThirdCharacterController.cs`
|
||||
### 5.5 编写脚本`ThirdCharacterController.cs`
|
||||
- 在 `Assets\Scripts\StudyCase1` 下创建脚本 `ThirdCharacterController`
|
||||
```csharp
|
||||
using UnityEngine;
|
||||
@@ -175,14 +175,14 @@ namespace StudyCase1
|
||||
}
|
||||
```
|
||||
|
||||
### 代码讲解
|
||||
### 5.6 代码讲解
|
||||
- **namespace StudyCase1**:因为可能出现同名文件
|
||||
- **forward**:作为“参照物”同步虚拟相机的 Y 轴旋转,用来转换输入的坐标轴。
|
||||
- **moveDir**:在本地坐标系下计算移动向量,并通过 `CharacterController.Move` 驱动。
|
||||
- **OnMove/OnJump**:直接使用 Input System 回调,确保只有在贴地时才写入输入,避免空中漂移。
|
||||
- **旋转插值**:`Quaternion.Slerp` 让角色转身更加顺滑,可根据手感微调 `turnSpeed`。
|
||||
|
||||
### Player节点设置
|
||||
### 5.7 Player节点设置
|
||||
- 挂载`ThirdCharacterController`
|
||||
|
||||
<img src="/image/studycase1/挂载ThirdCharacterController.png" data-fancybox="gallery"/>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: StudyCase2
|
||||
date: 2025-11-26 15:38
|
||||
date: 2025-11-27 16:35:05
|
||||
tags: [studycase, unity]
|
||||
pinned: true
|
||||
head:
|
||||
@@ -44,7 +44,7 @@ head:
|
||||
<img src="/image/studycase2/导入shader.png" data-fancybox="gallery"/>
|
||||
|
||||
## 5.勇者行迹录-章节任务
|
||||
### 创建StudyCase2
|
||||
### 5.1 创建StudyCase2
|
||||
- 复制场景和脚本改名为StudyCase2
|
||||
|
||||
<img src="/image/studycase2/复制场景和脚本改名为StudyCase2.png" data-fancybox="gallery"/>
|
||||
@@ -53,7 +53,7 @@ head:
|
||||
|
||||
<img src="/image/studycase2/修改控制器的命名空间.png" data-fancybox="gallery"/>
|
||||
|
||||
### 模型处理
|
||||
### 5.2 模型处理
|
||||
- 导出FBX材质球到Mats文件夹方便修改
|
||||
|
||||
<img src="/image/studycase2/导出材质.png" data-fancybox="gallery"/>
|
||||
@@ -70,7 +70,7 @@ head:
|
||||
|
||||
<img src="/image/studycase2/嘴部材质特殊处理.png" data-fancybox="gallery"/>
|
||||
|
||||
### 用模型替换之前的胶囊体
|
||||
### 5.3 用模型替换之前的胶囊体
|
||||
- 替换模型
|
||||
|
||||
<img src="/image/studycase2/替换模型.png" data-fancybox="gallery"/>
|
||||
@@ -79,7 +79,7 @@ head:
|
||||
|
||||
<img src="/image/studycase2/调整模型大小和位置.png" data-fancybox="gallery"/>
|
||||
|
||||
### 创建动画控制器,添加Idle和Move动画片段
|
||||
### 5.4 创建动画控制器,添加Idle和Move动画片段
|
||||
- 创建动画控制器
|
||||
|
||||
<img src="/image/studycase2/创建动画控制器.png" data-fancybox="gallery"/>
|
||||
@@ -100,7 +100,7 @@ head:
|
||||
|
||||
<img src="/image/studycase2/moving和idle设置为循环播放.png" data-fancybox="gallery"/>
|
||||
|
||||
### 创建状态条件
|
||||
### 5.5 创建状态条件
|
||||
- 设置idle到moving的条件
|
||||
|
||||
<img src="/image/studycase2/设置idle到moving的条件.png" data-fancybox="gallery"/>
|
||||
@@ -113,7 +113,7 @@ head:
|
||||
|
||||
<img src="/image/studycase2/设置move end到moving的条件.png" data-fancybox="gallery"/>
|
||||
|
||||
### 编写脚本`ThirdCharacterController.cs`
|
||||
### 5.6 编写脚本`ThirdCharacterController.cs`
|
||||
- 修改`Assets\Scripts\StudyCase2` 下脚本 `ThirdCharacterController.cs`
|
||||
```csharp
|
||||
using UnityEngine;
|
||||
@@ -156,11 +156,11 @@ namespace StudyCase2
|
||||
moveDir = forward.TransformDirection(moveDir);
|
||||
if (moveInput != Vector2.zero)
|
||||
{
|
||||
animator?.SetBool("Move", true);
|
||||
animator.SetBool("Move", true);
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
else animator?.SetBool("Move", false);
|
||||
else animator.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
@@ -181,7 +181,7 @@ namespace StudyCase2
|
||||
}
|
||||
}
|
||||
```
|
||||
### 代码讲解
|
||||
### 5.7代码讲解
|
||||
在Awake中自动获取组件,移除PlayerInput
|
||||
```csharp
|
||||
private void Awake()
|
||||
@@ -205,7 +205,7 @@ private void Awake()
|
||||
//false:播放Move end动画,然后回到Idle
|
||||
animator.SetBool("Move", true);
|
||||
```
|
||||
### Player节点设置
|
||||
### 5.8 Player节点设置
|
||||
挂载studycase2的控制器,移除PlayerInput组件
|
||||
<img src="/image/studycase2/重载控制器.png" data-fancybox="gallery"/>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: StudyCase3
|
||||
date: 2025-11-26 21:33
|
||||
date: 2025-11-27 16:36:05
|
||||
tags: [studycase, unity]
|
||||
pinned: true
|
||||
head:
|
||||
@@ -37,11 +37,11 @@ head:
|
||||
### 导入[资源](/resources/studycase3/case3.zip)(天空球,描边,道具模型)
|
||||
|
||||
## 5.勇者行迹录-章节任务
|
||||
### 创建StudyCase3
|
||||
### 5.1 创建StudyCase3
|
||||
- 复制场景和脚本改名为StudyCase3
|
||||
- 修改控制器的命名空间
|
||||
|
||||
### 道具
|
||||
### 5.2 道具
|
||||
- 把导入的道具模型放到场景中
|
||||
|
||||
<img src="/image/studycase3/把导入的道具模型放到场景中.png" data-fancybox="gallery"/>
|
||||
@@ -119,6 +119,7 @@ namespace StudyCase3
|
||||
|
||||
<img src="/image/studycase3/创建Pick输入事件,绑定F.png" data-fancybox="gallery"/>
|
||||
|
||||
### 5.3 Player脚本和设置
|
||||
- 修改`Assets\Scripts\StudyCase3` 下脚本 `ThirdCharacterController.cs`
|
||||
```csharp
|
||||
using UnityEngine;
|
||||
@@ -153,7 +154,7 @@ namespace StudyCase3
|
||||
public float maxDistance = 5f;
|
||||
public LayerMask layerMask;
|
||||
public float itemsRotateSpeed = 120f;
|
||||
public Transform itemRoot;
|
||||
[HideInInspector] public Transform itemRoot;
|
||||
Item selectItem;
|
||||
LineRenderer lineRenderer;
|
||||
private void Awake()
|
||||
@@ -205,11 +206,11 @@ namespace StudyCase3
|
||||
moveDir = forward.TransformDirection(moveDir);
|
||||
if (moveInput != Vector2.zero)
|
||||
{
|
||||
animator?.SetBool("Move", true);
|
||||
animator.SetBool("Move", true);
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
else animator?.SetBool("Move", false);
|
||||
else animator.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
|
||||
296
posts/studycase4.md
Normal file
@@ -0,0 +1,296 @@
|
||||
---
|
||||
title: studycase4
|
||||
date: 2025-11-30 12:40:05
|
||||
tags: [studycase, unity]
|
||||
pinned: true
|
||||
head:
|
||||
- - meta
|
||||
- name: description
|
||||
content: vitepress-theme-bluearchive studycase4
|
||||
- - meta
|
||||
- name: keywords
|
||||
content: vitepress theme bluearchive studycase4
|
||||
---
|
||||
|
||||
# 虚拟摇杆
|
||||
- [世界构筑的起始篇章-编辑器](#_1-世界构筑的起始篇章-编辑器)
|
||||
- [光芒汇聚之所-章节目标](#_2-光芒汇聚之所-章节目标)
|
||||
- [光流影卷-演示视频](#_3-光流影卷-演示视频)
|
||||
- [初始祭坛-前置准备](#_4-初始祭坛-前置准备)
|
||||
- [勇者行迹录-章节任务](#_5-勇者行迹录-章节任务)
|
||||
- [异闻录-FAQ](#_6-异闻录-faq)
|
||||
|
||||
## 1.世界构筑的起始篇章-编辑器
|
||||
- **Unity 2022.3.62f2**:
|
||||
- **Visual Studio 2022**
|
||||
|
||||
## 2.光芒汇聚之所-章节目标
|
||||
- **虚拟摇杆和按钮**
|
||||
- **In game Debug**
|
||||
- **打包APK**
|
||||
|
||||
## 3.光流影卷-演示视频
|
||||
<video id="vdMain" controls muted poster="/video/studycase4/演示视频.png" playsinline>
|
||||
<source id="vSource" src="/video/studycase4/演示视频.mp4" type="video/mp4" />
|
||||
</video>
|
||||
|
||||
## 4.初始祭坛-前置准备
|
||||
### 导入[资源](/resources/studycase4/case4.zip)(摇杆图标、Debug工具)
|
||||
|
||||
<img src="/image/studycase4/导入.png" data-fancybox="gallery"/>
|
||||
|
||||
### 导入InputSystem的OnScreen示例
|
||||
|
||||
<img src="/image/studycase4/onscreen.png" data-fancybox="gallery"/>
|
||||
|
||||
## 5.勇者行迹录-章节任务
|
||||
### 5.1 创建StudyCase4
|
||||
- 复制场景和脚本改名为studycase4
|
||||
- 修改控制器和道具的命名空间
|
||||
|
||||
### 5.2 IngameDebugConsole
|
||||
- 把IngameDebugConsole预制体拖入场景
|
||||
|
||||
<img src="/image/studycase4/debug.png" data-fancybox="gallery"/>
|
||||
|
||||
- 在Resources文件夹下创建材质Line,Shader选择Sprite-Unlit-Default
|
||||
|
||||
<img src="/image/studycase4/创建材质Line,设置shader为Sprite-Unlit-Default.png" data-fancybox="gallery"/>
|
||||
|
||||
### 5.3 虚拟摇杆创建
|
||||
- 全选mobile-controls-1下的Texture
|
||||
|
||||
<img src="/image/studycase4/全选mobile-controls-1下的Texture.png" data-fancybox="gallery"/>
|
||||
|
||||
- 切换到sprite(2d and ui)
|
||||
|
||||
<img src="/image/studycase4/切换到sprite.png" data-fancybox="gallery"/>
|
||||
|
||||
- 创建一个canvas,再创建一个image,命名为leftBg,sprite使用joystick_circle_pad_a,设置大小位置和锚点
|
||||
|
||||
<img src="/image/studycase4/创建leftBg.png" data-fancybox="gallery"/>
|
||||
|
||||
- 创建一个image,命名为leftStick,透明度0,设置大小位置和锚点,挂载On-Screen-Stick
|
||||
|
||||
<img src="/image/studycase4/创建leftStick.png" data-fancybox="gallery"/>
|
||||
|
||||
- 设置 `MovementRange` 为200,设置 `ControlPath` 为LeftStick
|
||||
|
||||
<img src="/image/studycase4/设置leftStick.png" data-fancybox="gallery"/>
|
||||
|
||||
- 复制一个leftBg改名为rightBg,设置大小位置和锚点
|
||||
|
||||
<img src="/image/studycase4/创建rightBg.png" data-fancybox="gallery"/>
|
||||
|
||||
- 复制一个leftStick改名为rightStick,设置`ControlPath` 为RightStick
|
||||
|
||||
<img src="/image/studycase4/创建并设置rightStick.png" data-fancybox="gallery"/>
|
||||
|
||||
- 添加输入事件,Move添加LeftStick,创建Look,添加Detla和RightStick
|
||||
|
||||
<img src="/image/studycase4/添加输入事件.gif" data-fancybox="gallery"/>
|
||||
|
||||
- 设置虚拟相机参数移动速度从300调到100,限制角度0到70
|
||||
|
||||
<img src="/image/studycase4/设置虚拟相机参数.png" data-fancybox="gallery"/>
|
||||
|
||||
- 修改虚拟相机输入
|
||||
|
||||
<img src="/image/studycase4/配置虚拟相机输入.gif" data-fancybox="gallery"/>
|
||||
|
||||
### 5.4 Player脚本和设置
|
||||
- 修改`Assets\Scripts\studycase4` 下脚本 `ThirdCharacterController.cs`
|
||||
```csharp
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Cinemachine;
|
||||
|
||||
namespace StudyCase4
|
||||
{
|
||||
public enum PickMode
|
||||
{
|
||||
RayCast,
|
||||
Mouse
|
||||
}
|
||||
public class ThirdCharacterController : MonoBehaviour
|
||||
{
|
||||
[Header("MoveSettings")]
|
||||
CharacterController characterController;
|
||||
InputActionAsset inputAction;
|
||||
Animator animator;
|
||||
Transform forward;
|
||||
Transform model;
|
||||
CinemachineVirtualCamera vCam;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
[Header("PickSettings")]
|
||||
public PickMode pickMode = PickMode.RayCast;
|
||||
public CursorLockMode cursorLock;
|
||||
public bool useDrawRay = true;
|
||||
public float maxDistance = 5f;
|
||||
public LayerMask layerMask;
|
||||
public float itemsRotateSpeed = 120f;
|
||||
public float minCameraDistance = 2f;
|
||||
public float maxCameraDistance = 10f;
|
||||
public float cameraZoomSpeed = 0.1f;
|
||||
[HideInInspector] public Transform itemRoot;
|
||||
Item selectItem;
|
||||
LineRenderer lineRenderer;
|
||||
CinemachineFramingTransposer framingTransposer;
|
||||
private void Awake()
|
||||
{
|
||||
Cursor.lockState = cursorLock;
|
||||
characterController = GetComponent<CharacterController>();
|
||||
forward = transform.Find("Forward");
|
||||
model = transform.Find("Model");
|
||||
animator = model.GetComponentInChildren<Animator>();
|
||||
vCam = transform.Find("Virtual Camera").GetComponent<CinemachineVirtualCamera>();
|
||||
framingTransposer = vCam.GetCinemachineComponent<CinemachineFramingTransposer>();
|
||||
inputAction = Resources.Load<InputActionAsset>("PlayerInputActions");
|
||||
inputAction.FindAction("Move").started += OnMove;
|
||||
inputAction.FindAction("Move").performed += OnMove;
|
||||
inputAction.FindAction("Move").canceled += OnMove;
|
||||
inputAction.FindAction("Jump").performed += OnJump;
|
||||
inputAction.FindAction("Pick").performed += OnPickUp;
|
||||
inputAction.FindAction("Zoom").started += OnZoom;
|
||||
inputAction.FindAction("Zoom").performed += OnZoom;
|
||||
inputAction.FindAction("Zoom").canceled += OnZoom;
|
||||
inputAction.Enable();
|
||||
itemRoot = transform.Find("ItemRoot");
|
||||
if (lineRenderer == null)
|
||||
{
|
||||
lineRenderer = gameObject.AddComponent<LineRenderer>();
|
||||
lineRenderer.material = Resources.Load<Material>("Line");
|
||||
lineRenderer.useWorldSpace = true;
|
||||
}
|
||||
layerMask = LayerMask.GetMask("Item");
|
||||
//开启多点触控
|
||||
Input.multiTouchEnabled = true;
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
Move();
|
||||
CheckRayHit();
|
||||
ItemsRotate();
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext context)
|
||||
{
|
||||
if (characterController.isGrounded)
|
||||
moveInput = context.ReadValue<Vector2>();
|
||||
else moveInput = Vector2.zero;
|
||||
}
|
||||
public void OnJump(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.performed && characterController.isGrounded)
|
||||
{
|
||||
moveDir.y = jumpSpeed;
|
||||
}
|
||||
}
|
||||
//增加控制相机距离的方法
|
||||
public void OnZoom(InputAction.CallbackContext context)
|
||||
{
|
||||
float scrollValue = context.ReadValue<float>();
|
||||
float newDistance = framingTransposer.m_CameraDistance + (scrollValue * cameraZoomSpeed * 0.01f);
|
||||
framingTransposer.m_CameraDistance = Mathf.Clamp(newDistance, minCameraDistance, maxCameraDistance);
|
||||
}
|
||||
void Move()
|
||||
{
|
||||
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
||||
forward.eulerAngles = new Vector3(0, vCam.transform.eulerAngles.y, 0);
|
||||
moveDir = forward.TransformDirection(moveDir);
|
||||
if (moveInput != Vector2.zero)
|
||||
{
|
||||
animator.SetBool("Move", true);
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
else animator.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
#region PickUp
|
||||
public void OnPickUp(InputAction.CallbackContext context)
|
||||
{
|
||||
if (pickMode != PickMode.RayCast) return;
|
||||
if (selectItem != null)
|
||||
selectItem.PickUp(itemRoot);
|
||||
selectItem = null;
|
||||
}
|
||||
void CheckRayHit()
|
||||
{
|
||||
lineRenderer.enabled = useDrawRay;
|
||||
if (pickMode != PickMode.RayCast)
|
||||
{
|
||||
useDrawRay = false;
|
||||
return;
|
||||
}
|
||||
Ray ray = new Ray(model.position, model.forward);
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
|
||||
{
|
||||
DrawRay(model.position, hit.point, new Color(0, 1, 1));
|
||||
Item curItem = hit.transform.GetComponent<Item>();
|
||||
if (curItem != null && curItem != selectItem)
|
||||
{
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = curItem;
|
||||
selectItem.Select();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawRay(model.position, model.position+ model.forward* maxDistance, new Color(1, 1, 1));
|
||||
if (selectItem != null)
|
||||
selectItem.UnSelect();
|
||||
selectItem = null;
|
||||
}
|
||||
}
|
||||
void DrawRay(Vector3 startPos, Vector3 endPos, Color color)
|
||||
{
|
||||
lineRenderer.startColor = lineRenderer.endColor = color;
|
||||
lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
|
||||
lineRenderer.positionCount = 2;
|
||||
lineRenderer.SetPosition(0, startPos);
|
||||
lineRenderer.SetPosition(1, endPos);
|
||||
lineRenderer.enabled = true;
|
||||
}
|
||||
void ItemsRotate()
|
||||
{
|
||||
itemRoot.Rotate(0,itemsRotateSpeed*Time.deltaTime, 0);
|
||||
}
|
||||
public void SetItems()
|
||||
{
|
||||
float radius = 1.5f;
|
||||
float angleStep = 360 / itemRoot.childCount;
|
||||
for (int i = 0; i < itemRoot.childCount; i++)
|
||||
{
|
||||
Transform item = itemRoot.GetChild(i);
|
||||
float angle = angleStep * i;
|
||||
Quaternion rot = Quaternion.Euler(0, angle, 0);
|
||||
item.localPosition = rot * itemRoot.forward * radius;
|
||||
item.localScale = Vector3.one * 0.1f;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
```
|
||||
### 5.5打包并测试
|
||||
- 玩家移除case3的脚本,挂载case4脚本
|
||||
- BuildSettings切换到Android平台
|
||||
|
||||
<img src="/image/studycase4/切换到Android平台.png" data-fancybox="gallery"/>
|
||||
|
||||
- 打包apk
|
||||
|
||||
<img src="/image/studycase4/打包apk.png" data-fancybox="gallery"/>
|
||||
|
||||
- 在手机上安装和运行
|
||||
|
||||
## 6.异闻录-FAQ
|
||||
198
posts/studycase5.md
Normal file
@@ -0,0 +1,198 @@
|
||||
---
|
||||
title: studycase5
|
||||
date: 2025-12-2 17:23:05
|
||||
tags: [studycase, unity]
|
||||
pinned: true
|
||||
head:
|
||||
- - meta
|
||||
- name: description
|
||||
content: vitepress-theme-bluearchive studycase5
|
||||
- - meta
|
||||
- name: keywords
|
||||
content: vitepress theme bluearchive studycase5
|
||||
---
|
||||
|
||||
# 虚拟摇杆
|
||||
- [世界构筑的起始篇章-编辑器](#_1-世界构筑的起始篇章-编辑器)
|
||||
- [光芒汇聚之所-章节目标](#_2-光芒汇聚之所-章节目标)
|
||||
- [光流影卷-演示视频](#_3-光流影卷-演示视频)
|
||||
- [初始祭坛-前置准备](#_4-初始祭坛-前置准备)
|
||||
- [勇者行迹录-章节任务](#_5-勇者行迹录-章节任务)
|
||||
- [异闻录-FAQ](#_6-异闻录-faq)
|
||||
|
||||
## 1.世界构筑的起始篇章-编辑器
|
||||
- **Unity 2022.3.62f2**:
|
||||
- **Visual Studio 2022**
|
||||
|
||||
## 2.光芒汇聚之所-章节目标
|
||||
- **使用Mirror实现简单联机**
|
||||
- **打包exe**
|
||||
|
||||
## 3.光流影卷-演示视频
|
||||
<video id="vdMain" controls muted poster="/video/studycase5/演示视频.png" playsinline>
|
||||
<source id="vSource" src="/video/studycase5/演示视频.mp4" type="video/mp4" />
|
||||
</video>
|
||||
|
||||
## 4.初始祭坛-前置准备
|
||||
### 导入[资源](/resources/studycase5/case5.zip)(Mirror)
|
||||
|
||||
<img src="/image/studycase5/导入Mirror.png" data-fancybox="gallery"/>
|
||||
<img src="/image/studycase5/导入Mirror步骤2.png" data-fancybox="gallery"/>
|
||||
<img src="/image/studycase5/导入Mirror步骤3.png" data-fancybox="gallery"/>
|
||||
|
||||
## 5.勇者行迹录-章节任务
|
||||
### 5.1 创建StudyCase5
|
||||
|
||||
- 复制场景改名为StudyCase5
|
||||
- 删除道具,Player移除ThirdCharacterController
|
||||
|
||||
### 5.2 了解 `Mirror`
|
||||
|
||||
- 打开示例Tanks场景
|
||||
|
||||
<img src="/image/studycase5/打开示例Tanks场景.png" data-fancybox="gallery"/>
|
||||
|
||||
- 为了查看联机效果,修改项目设置
|
||||
|
||||
<img src="/image/studycase5/Windows项目设置.png" data-fancybox="gallery"/>
|
||||
|
||||
- 把示例打包成exe
|
||||
|
||||
<img src="/image/studycase5/打包Windows.png" data-fancybox="gallery"/>
|
||||
|
||||
- 打开俩个exe,一个选择host,另一个选择client
|
||||
|
||||
<img src="/image/studycase5/打开host和client.png" data-fancybox="gallery"/>
|
||||
|
||||
- 体验联机效果
|
||||
|
||||
<img src="/image/studycase5/体验联机.png" data-fancybox="gallery"/>
|
||||
|
||||
### 5.2 使用 `Mirror`
|
||||
|
||||
- 首先将示例场景(MirrorTanks)中的网络控制器(NetworkManager)复制到我们的场景中(StudyCase5)
|
||||
|
||||
<img src="/image/studycase5/复制NetworkManager.png" data-fancybox="gallery"/>
|
||||
|
||||
- Player添加NetworkIdentity组件(联网对象需要这个组件)
|
||||
|
||||
<img src="/image/studycase5/Player添加NetworkIdentity组件.png" data-fancybox="gallery"/>
|
||||
|
||||
- 添加俩个NetworkTransform(Transform同步需要这个组件)
|
||||
|
||||
<img src="/image/studycase5/添加俩个NetworkTransform.png" data-fancybox="gallery"/>
|
||||
|
||||
- 设置NetworkTransform(Player和Model)
|
||||
|
||||
<img src="/image/studycase5/设置NetworkTransform.png" data-fancybox="gallery"/>
|
||||
|
||||
- Player添加NetworkAnimator组件并设置(动画状态同步需要这个组件)
|
||||
|
||||
<img src="/image/studycase5/Player添加NetworkAnimator组件并设置.png" data-fancybox="gallery"/>
|
||||
|
||||
- PlayerInputActions中创建Esc的监听事件
|
||||
|
||||
<img src="/image/studycase5/添加ESC.png" data-fancybox="gallery"/>
|
||||
|
||||
- 创建NetworkBehaviour,命名为 `NetWorkPlayer`
|
||||
|
||||
<img src="/image/studycase5/创建NetworkBehaviour.png" data-fancybox="gallery"/>
|
||||
|
||||
- 编写脚本 `NetWorkPlayer`
|
||||
```csharp
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
using UnityEngine.InputSystem;
|
||||
using Cinemachine;
|
||||
|
||||
public class NetWorkPlayer : NetworkBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public InputActionAsset inputAction;
|
||||
public Animator animator;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public CinemachineVirtualCamera vCam;
|
||||
public CursorLockMode cursorLock;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
|
||||
public override void OnStartLocalPlayer()
|
||||
{
|
||||
Cursor.lockState = cursorLock;
|
||||
vCam.Priority = 15;
|
||||
inputAction.FindAction("Move").started += OnMove;
|
||||
inputAction.FindAction("Move").performed += OnMove;
|
||||
inputAction.FindAction("Move").canceled += OnMove;
|
||||
inputAction.FindAction("Jump").performed += OnJump;
|
||||
inputAction.FindAction("Esc").performed += OnEsc;
|
||||
inputAction.Enable();
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
||||
forward.eulerAngles = new Vector3(0, vCam.transform.eulerAngles.y, 0);
|
||||
moveDir = forward.TransformDirection(moveDir);
|
||||
if (moveInput != Vector2.zero)
|
||||
{
|
||||
animator.SetBool("Move", true);
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
else animator.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
if (characterController.isGrounded)
|
||||
moveInput = context.ReadValue<Vector2>();
|
||||
else moveInput = Vector2.zero;
|
||||
}
|
||||
public void OnJump(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
if (characterController.isGrounded)
|
||||
{
|
||||
moveDir.y = jumpSpeed;
|
||||
}
|
||||
}
|
||||
public void OnEsc(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
if(Cursor.lockState == CursorLockMode.Locked)
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
else
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Player挂载 `NetWorkPlayer` ,Player的设置
|
||||
|
||||
<img src="/image/studycase5/Player的设置.png" data-fancybox="gallery"/>
|
||||
|
||||
- 将Player保存为预制体(拖拽到文件夹中会自动保存为预制体)
|
||||
|
||||
<img src="/image/studycase5/保存玩家Prefab.png" data-fancybox="gallery"/>
|
||||
|
||||
- 删除场景中的Player,后续由NetworkManager创建,创建俩个出生点,挂载 `NetworkStartPosition`
|
||||
|
||||
<img src="/image/studycase5/设置场景.png" data-fancybox="gallery"/>
|
||||
|
||||
- 将预制体Player拖拽到NetworkManager上的 `PlayerPrefab`
|
||||
|
||||
<img src="/image/studycase5/player替换tank.png" data-fancybox="gallery"/>
|
||||
|
||||
- 打包exe,打开俩个体验联机
|
||||
|
||||
<img src="/image/studycase5/体验联机2.png" data-fancybox="gallery"/>
|
||||
|
||||
## 6.异闻录-FAQ
|
||||
BIN
public/image/studycase4/debug.png
Normal file
|
After Width: | Height: | Size: 912 KiB |
BIN
public/image/studycase4/onscreen.png
Normal file
|
After Width: | Height: | Size: 394 KiB |
BIN
public/image/studycase4/全选mobile-controls-1下的Texture.png
Normal file
|
After Width: | Height: | Size: 696 KiB |
BIN
public/image/studycase4/切换到Android平台.png
Normal file
|
After Width: | Height: | Size: 70 KiB |
BIN
public/image/studycase4/切换到sprite.png
Normal file
|
After Width: | Height: | Size: 682 KiB |
BIN
public/image/studycase4/创建leftBg.png
Normal file
|
After Width: | Height: | Size: 515 KiB |
BIN
public/image/studycase4/创建leftStick.png
Normal file
|
After Width: | Height: | Size: 432 KiB |
BIN
public/image/studycase4/创建rightBg.png
Normal file
|
After Width: | Height: | Size: 538 KiB |
BIN
public/image/studycase4/创建并设置rightStick.png
Normal file
|
After Width: | Height: | Size: 520 KiB |
|
After Width: | Height: | Size: 591 KiB |
BIN
public/image/studycase4/导入.png
Normal file
|
After Width: | Height: | Size: 24 KiB |
BIN
public/image/studycase4/打包apk.png
Normal file
|
After Width: | Height: | Size: 360 KiB |
BIN
public/image/studycase4/添加输入事件.gif
Normal file
|
After Width: | Height: | Size: 728 KiB |
BIN
public/image/studycase4/设置leftStick.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
public/image/studycase4/设置虚拟相机参数.png
Normal file
|
After Width: | Height: | Size: 307 KiB |
BIN
public/image/studycase4/配置虚拟相机输入.gif
Normal file
|
After Width: | Height: | Size: 303 KiB |
BIN
public/image/studycase5/Player添加NetworkAnimator组件并设置.png
Normal file
|
After Width: | Height: | Size: 693 KiB |
BIN
public/image/studycase5/Player添加NetworkIdentity组件.png
Normal file
|
After Width: | Height: | Size: 655 KiB |
BIN
public/image/studycase5/Player的设置.png
Normal file
|
After Width: | Height: | Size: 903 KiB |
BIN
public/image/studycase5/Windows项目设置.png
Normal file
|
After Width: | Height: | Size: 378 KiB |
BIN
public/image/studycase5/player替换tank.png
Normal file
|
After Width: | Height: | Size: 667 KiB |
BIN
public/image/studycase5/体验联机.png
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
BIN
public/image/studycase5/体验联机2.png
Normal file
|
After Width: | Height: | Size: 619 KiB |
BIN
public/image/studycase5/保存玩家Prefab.png
Normal file
|
After Width: | Height: | Size: 919 KiB |
BIN
public/image/studycase5/创建NetworkBehaviour.png
Normal file
|
After Width: | Height: | Size: 508 KiB |
BIN
public/image/studycase5/复制NetworkManager.png
Normal file
|
After Width: | Height: | Size: 64 KiB |
BIN
public/image/studycase5/导入Mirror.png
Normal file
|
After Width: | Height: | Size: 384 KiB |
BIN
public/image/studycase5/导入Mirror步骤2.png
Normal file
|
After Width: | Height: | Size: 546 KiB |
BIN
public/image/studycase5/导入Mirror步骤3.png
Normal file
|
After Width: | Height: | Size: 593 KiB |
BIN
public/image/studycase5/打包Windows.png
Normal file
|
After Width: | Height: | Size: 332 KiB |
BIN
public/image/studycase5/打开host和client.png
Normal file
|
After Width: | Height: | Size: 1.0 MiB |
BIN
public/image/studycase5/打开示例Tanks场景.png
Normal file
|
After Width: | Height: | Size: 1.0 MiB |
BIN
public/image/studycase5/添加ESC.png
Normal file
|
After Width: | Height: | Size: 44 KiB |
BIN
public/image/studycase5/添加俩个NetworkTransform.png
Normal file
|
After Width: | Height: | Size: 656 KiB |
BIN
public/image/studycase5/设置NetworkTransform.png
Normal file
|
After Width: | Height: | Size: 761 KiB |
BIN
public/image/studycase5/设置场景.png
Normal file
|
After Width: | Height: | Size: 880 KiB |
BIN
public/resources/studycase4/case4.zip
Normal file
BIN
public/resources/studycase5/case5.zip
Normal file
BIN
public/video/studycase4/演示视频.mp4
Normal file
BIN
public/video/studycase4/演示视频.png
Normal file
|
After Width: | Height: | Size: 574 KiB |
BIN
public/video/studycase5/演示视频.mp4
Normal file
BIN
public/video/studycase5/演示视频.png
Normal file
|
After Width: | Height: | Size: 471 KiB |