C#元组和Unity Vector3

C#元组和Unity Vector3详解

一、C# 元组(Tuple)

1. 基本概念

  • 元组是一种轻量级的数据结构
  • 可以存储多个不同类型的值
  • C# 7.0及以后版本支持更简洁的语法
  • 支持命名和解构

2. 创建方式

csharp 复制代码
// 方式1:使用Tuple类
Tuple<int, string> tuple1 = new Tuple<int, string>(1, "Hello");

// 方式2:使用简写语法(C# 7.0+)
(int id, string name) tuple2 = (1, "Hello");

// 方式3:类型推断
var tuple3 = (id: 1, name: "Hello");

3. 元组特性

  • 值类型(存储在栈上)
  • 成员不可变(但可以整体重新赋值)
  • 支持相等比较
  • 可作为方法返回值

4. 常用操作

csharp 复制代码
// 访问元素
(int x, string y) tuple = (123, "test");
Console.WriteLine(tuple.x);      // 使用命名
Console.WriteLine(tuple.Item1);  // 使用默认Item名

// 解构
var (id, name) = GetPerson();

5. 实际应用场景

csharp 复制代码
// 返回多个值
(int min, int max) FindMinMax(int[] numbers)
{
    return (numbers.Min(), numbers.Max());
}

// 作为字典键
Dictionary<(int, int), float> gridValues;

// 临时数据组合
var playerInfo = (health: 100, position: new Vector3(0,0,0));

二、Unity Vector3

1. 基本概念

  • 表示3D空间中的点或方向
  • 包含X、Y、Z三个浮点数分量
  • 结构体类型(值类型)
  • Unity中最常用的数据类型之一

2. 常用属性

csharp 复制代码
// 静态属性
Vector3.zero     // (0, 0, 0)
Vector3.one      // (1, 1, 1)
Vector3.up       // (0, 1, 0)
Vector3.down     // (0, -1, 0)
Vector3.left     // (-1, 0, 0)
Vector3.right    // (1, 0, 0)
Vector3.forward  // (0, 0, 1)
Vector3.back     // (0, 0, -1)

// 实例属性
vector.magnitude     // 向量长度
vector.sqrMagnitude // 向量长度的平方
vector.normalized   // 归一化后的向量

3. 常用方法

csharp 复制代码
// 静态方法
Vector3.Distance(a, b)    // 两点间距离
Vector3.Angle(a, b)       // 两向量夹角
Vector3.Dot(a, b)        // 点积
Vector3.Cross(a, b)      // 叉积
Vector3.Lerp(a, b, t)    // 线性插值
Vector3.Slerp(a, b, t)   // 球形插值
Vector3.Project(a, b)    // 向量投影
Vector3.Reflect(a, normal) // 反射

// 实例方法
vector.Normalize()       // 归一化
vector.Set(x, y, z)     // 设置分量值
vector.ToString()       // 转换为字符串

4. 运算符重载

csharp 复制代码
// 加减
Vector3 c = a + b;
Vector3 d = a - b;

// 乘除
Vector3 e = a * 2f;
Vector3 f = a / 2f;

// 取反
Vector3 g = -a;

5. 常见使用场景

位置操作
csharp 复制代码
// 移动物体
transform.position += Vector3.forward * speed * Time.deltaTime;

// 计算方向
Vector3 direction = target.position - transform.position;
旋转操作
csharp 复制代码
// 旋转物体
transform.rotation = Quaternion.LookRotation(direction);

// 插值旋转
transform.rotation = Quaternion.Slerp(
    transform.rotation,
    targetRotation,
    Time.deltaTime * rotateSpeed
);
物理计算
csharp 复制代码
// 添加力
rigidbody.AddForce(Vector3.up * jumpForce);

// 速度设置
rigidbody.velocity = new Vector3(x, y, z);

6. 性能优化建议

  1. 计算优化
csharp 复制代码
// 优化前
if (Vector3.Distance(a, b) < range)

// 优化后(避免开方运算)
if ((a - b).sqrMagnitude < range * range)
  1. 内存优化
csharp 复制代码
// 避免频繁创建新的Vector3
private Vector3 moveDirection;
void Update() {
    moveDirection.Set(x, y, z);
    transform.position += moveDirection * speed * Time.deltaTime;
}

7. 最佳实践

  1. 向量运算
csharp 复制代码
// 标准化方向向量
Vector3 direction = (target.position - transform.position).normalized;

// 使用点积判断方向
float dot = Vector3.Dot(transform.forward, direction);
bool isInFront = dot > 0;
  1. 插值应用
csharp 复制代码
// 平滑移动
transform.position = Vector3.Lerp(
    transform.position,
    targetPosition,
    smoothSpeed * Time.deltaTime
);

// 相机跟随
Vector3 desiredPosition = target.position + offset;
transform.position = Vector3.SmoothDamp(
    transform.position,
    desiredPosition,
    ref velocity,
    smoothTime
);

总结

  1. 元组(Tuple)
  • 适合临时组合多个值
  • 方法返回多个值时很有用
  • 注意性能开销,不适合频繁创建
  1. Vector3
  • Unity 3D开发中最基础的数据类型
  • 掌握常用属性和方法
  • 注意性能优化
  • 合理使用插值实现平滑效果
相关推荐
唐青枫1 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech2 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf3 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6254 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech4 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm