创建标签系统包
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# Unity UI Label Follow System
|
||||
|
||||
## 概述
|
||||
|
||||
这个项目展示了如何在Unity中创建一个UI标签,并使其在世界空间中跟随一个目标物体。标签会根据相机距离进行缩放。
|
||||
|
||||
## 文件列表
|
||||
|
||||
- `LabelFollower.cs`:用于使标签跟随目标物体,并根据相机距离进行缩放。
|
||||
- `LabelController.cs`:用于创建和管理标签的静态管理器类。
|
||||
|
||||
## 使用步骤
|
||||
|
||||
1. **创建一个Canvas**:
|
||||
- 在Unity编辑器中,右键点击层级视图,选择`UI -> Canvas`创建一个Canvas。
|
||||
- 确保Canvas的`Render Mode`设置为`World Space`。
|
||||
|
||||
2. **创建一个标签预制件**:
|
||||
- 在Canvas下创建一个`UI -> Image`对象作为标签的背景。
|
||||
- 设置图片的样式。
|
||||
- 在Image对象下创建一个`UI -> Text`对象,作为标签的文本内容。
|
||||
- 设置文本的内容和样式。
|
||||
- 将包含Image和Text的标签对象拖动到项目窗口中以创建一个预制件,然后删除层级视图中的标签对象。
|
||||
|
||||
3. **创建LabelController**:
|
||||
- 在一个空的GameObject上添加`LabelController`脚本。
|
||||
- 在脚本的Inspector面板中,设置`Canvas`为包含标签的Canvas对象。
|
||||
|
||||
4. **使用LabelController创建标签**:
|
||||
- 你可以在其他脚本中使用`LabelController.CreateLabel`方法来创建标签。例如:
|
||||
|
||||
```csharp
|
||||
using UnityEngine;
|
||||
|
||||
public class ExampleUsage : MonoBehaviour
|
||||
{
|
||||
public Transform target;
|
||||
public GameObject labelPrefab;
|
||||
|
||||
void Start()
|
||||
{
|
||||
LabelController.CreateLabel(target, labelPrefab, new Vector3(0, 2, 0), 1.0f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7978ad7d6c868b142b0d9d612c64c8f7
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d67216a97f88454095422146db2f609
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua
|
||||
{
|
||||
public class LabelController : MonoBehaviour
|
||||
{
|
||||
public static LabelController Instance { get; private set; }
|
||||
|
||||
public Canvas canvas; // 包含标签的Canvas
|
||||
public GameObject labelPrefab; // 标签预制体
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public static GameObject CreateLabel(Transform target, Vector3 offset)
|
||||
{
|
||||
return CreateLabel(target, Instance.labelPrefab, offset);
|
||||
}
|
||||
public static GameObject CreateLabel(Transform target, GameObject labelPrefab, Vector3 offset)
|
||||
{
|
||||
GameObject labelObject = Instantiate(labelPrefab, Instance.canvas.transform);
|
||||
LabelFollower followObjectLabel = labelObject.GetComponent<LabelFollower>();
|
||||
followObjectLabel.target = target;
|
||||
followObjectLabel.offset = offset;
|
||||
|
||||
return labelObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7eb8180a2e59e44f94464ec35f42b5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua
|
||||
{
|
||||
public class LabelFollower : MonoBehaviour
|
||||
{
|
||||
public Transform target; // 要跟随的目标物体
|
||||
public Vector3 offset; // 标签的偏移量
|
||||
void Update()
|
||||
{
|
||||
if (target != null)
|
||||
{
|
||||
// 设置标签的位置
|
||||
transform.position = target.position + offset;
|
||||
|
||||
// 使标签面向相机
|
||||
transform.rotation = Camera.main.transform.rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 485994f2ed6f1e448bea192023877815
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "MuHua"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ea4e4c7b125e9d4e953b91e32da550b
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "muhua-label-follow",
|
||||
"version": "1.0.0",
|
||||
"displayName": "MuHua LabelFollow",
|
||||
"description": "\u6d6e\u52a8\u6807\u7b7e\u7cfb\u7edf",
|
||||
"author": {
|
||||
"name": "MuHua",
|
||||
"email": "2960208585@qq.com"
|
||||
},
|
||||
"type": "tool",
|
||||
"samples": [
|
||||
{
|
||||
"displayName": "Label Follow Example",
|
||||
"description": "An example showing how to use the Label Follow system.",
|
||||
"path": "Samples~"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fc14c1f05b41784eacf23e52739a5cc
|
||||
PackageManifestImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user