白泽图

  • 文章
    • Unity渲染
    • Unity项目开发
    • 工具
    • 数学
    • 算法
    • 网站搭建
    • 网络&操作系统
蒋程个人博客
互联网技术经验总结&分享
  1. 首页
  2. Unity项目开发
  3. 正文

Unity坐标转换

2021-03-02 945点热度 0人点赞 0条评论

在Unity中经常用到本地坐标(相对坐标)与世界坐标的转换,比如在开发某些UI组件时,本文整理了相关转换接口

测试层级如下图:

测试代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JCTest_Trans : MonoBehaviour
{
    public Transform mTestPoint;
    void Start()
    {
        //本地坐标(相对于Point_Parent2)
        Debug.Log(mTestPoint.localPosition);

        var Point_Parent2 = mTestPoint.parent;
        Debug.LogFormat("本地坐标(相对于Point_Parent2表式方法1):{0}", Point_Parent2.worldToLocalMatrix.MultiplyPoint(mTestPoint.position));
        Debug.LogFormat("本地坐标(相对于Point_Parent2表式方法2):{0}", Point_Parent2.InverseTransformPoint(mTestPoint.position));

        //本地坐标(在其他节点下的本地坐标)
        var Point_Parent1 = mTestPoint.parent.parent;
        Debug.LogFormat("本地坐标(相对于Point_Parent1表式方法1):{0}", Point_Parent1.worldToLocalMatrix.MultiplyPoint(mTestPoint.position));
        Debug.LogFormat("本地坐标(相对于Point_Parent1表式方法2):{0}", Point_Parent1.InverseTransformPoint(mTestPoint.position));

        //世界坐标
        Debug.Log(mTestPoint.position);

        //世界坐标(其他表示方法)
        Debug.LogFormat("世界坐标(表式方法1):{0}", mTestPoint.parent.localToWorldMatrix.MultiplyPoint(mTestPoint.localPosition));
        Debug.LogFormat("世界坐标(表式方法2):{0}", mTestPoint.parent.TransformPoint(mTestPoint.localPosition));

    }
}

打印测试结果:

标签: 暂无
最后更新:2021-03-02

蒋程

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

COPYRIGHT © 2023 白泽图. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

登录
注册|忘记密码?