Unity 按顺序拆解组装模型

1.先看效果图,按照自己需要的顺序拆解组装模型

 2.将需要移动的模型按照移动的顺序加入到 ObiController 数组中

 3.将需要移动到的位置赋值给需要移动的模型上,并对应好位置关系

 4.对应脚本,仅供参考,欢迎交流分享.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
//挂载到需要移动位置的模型上
public class ObiTransform : MonoBehaviour
{
    //目标位置
    public GameObject targetPoint;
    private Vector3 targetPosi;
    private Vector3 targetRote;

    private Vector3 oldPosi;


    private void Start()
    {
        targetPosi = targetPoint.transform.localPosition;
        targetRote = targetPoint.transform.eulerAngles;
        oldPosi = transform.localPosition;
    }


    public void Open()
    {
        transform.DOLocalMove(targetPosi, 0.5f);
    }
    public void Close()
    {
        transform.DOLocalMove(oldPosi, 0.5f);
    }
}
using UnityEngine;

public class ObiController : MonoBehaviour
{
    [SerializeField] private GameObject[] target;

    private int index = -1;
    /// <summary>
    /// 合并
    /// </summary>
    public void MergeObject()
    {
        if (play)
        {
            play = false;
        }
        else
        {
            play = true;
        }

        if (j == 0)
        {
            j = 1;

        }

        if (j == 10)
        {
            j = 20;
        }
   
    }
    /// <summary>
    /// 拆分
    /// </summary>
    public void SplitObject()
    {
        if (play)
        {
            play = false;
            Debug.Log(play);
        }
        else
        {
            play = true;
            Debug.Log(play);
        }

        if (j == 0)
        {
            j = 1;

        }

        if (j == 10)
        {
            j = 20;
        }
    }
    float j = 0;
    float ii = 0.5f;
    public static bool play;
    private void Update()
    {
        ii -= Time.deltaTime;

        if (ii <= 0 && play)
        {
            if (j == 1)
            {
                if (index < target.Length - 1)
                {
                    index++;
                }
                else
                {
                    j = 10;
                    play = false;
                  
                }

                if (index > -1 && index < target.Length)
                {
                    Debug.Log(index);
                    target[index].GetComponent<ObiTransform>().Open();
                }
            }

            if (j == 20)
            {
                if (index > -1 && index < target.Length)
                {
                    target[index].GetComponent<ObiTransform>().Close();
                }

                if (index > -1)
                {
                    index--;
                }
                else
                {
                    j = 0;
                    play = false;
                   
                }
            }
            ii = 0.35f;
        }
    }
}

  • 12
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Unity中进行模型拆解是指对一个整体模型进行拆分,将其分解为更小精细的部分。这样的操作可以使模型在游戏开发、建模制作中更加灵活、方便。 首先,为了对模型进行拆解,我们需要导入一个整体模型文件,这可以是一个3D建模软件中制作的模型,如Blender、Maya等。在导入模型后,我们可以在Unity的Hierarchy面板中找到该模型的GameObject对象。 其次,在模型的GameObject对象上,我们可以选择使用Unity的编辑模式来进行拆解。在编辑模式下,我们可以选中一个特定的子物体,然后使用Unity的工具栏中的编辑操作、移动、旋转、缩放等功能来调整子物体的位置和大小。 另外,Unity还提供了一种更具可视化效果的方式来进行模型拆解,即使用预制体(Prefab)。预制体是一种将对象和其所有组件打包在一起的特殊对象,可以在场景中复用。我们可以将整体模型拆分为多个子物体,然后将每个子物体制作成预制体,方便在需要的地方进行快速加载和使用。 最后,在模型进行拆解后,我们可以将它们分别保存为不同的文件,并在需要的时候将其导入到Unity中。这样,在开发中,我们可以更加方便地调整和组合不同的模型部分,以实现更加复杂和精细的效果。 总结起来,Unity中的模型拆解可以通过编辑模式和预制体的使用来实现。这种操作可以使模型在游戏开发和建模制作中更加灵活、方便,提高开发效率。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值