Unity PhysicsScene测试

应该是unity 2018.3中加入的功能,对象能够放置于不一样的物理场景中。git

一个Scene对应一个物理场景(PhysicsScene),若想放入独立的物理场景测试建立一个Scene便可。见下图gif。测试

 

若测试模拟功能,须在Physics中关闭自动模拟。脚本以下:spa

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

public class PhysicsSceneDemo : MonoBehaviour
{
    public GameObject[] simulationObjects;
    public float physicsSceneTime;

    float mLastPhysicsSceneTime;
    Scene mPhysicsTestScene;
    PhysicsScene mPhysicsScene;


    void Start()
    {
        mPhysicsTestScene = SceneManager.CreateScene("PhysicsTestScene");
        mPhysicsScene = mPhysicsTestScene.GetPhysicsScene();

        for (int i = 0; i < simulationObjects.Length; i++)
            SceneManager.MoveGameObjectToScene(simulationObjects[i], mPhysicsTestScene);

        Physics.autoSimulation = false;
    }

    void Update()
    {
        if (physicsSceneTime != mLastPhysicsSceneTime)
        {
            mPhysicsScene.Simulate(physicsSceneTime);
            mLastPhysicsSceneTime = physicsSceneTime;
        }
    }

    void OnDestroy()
    {
        SceneManager.UnloadSceneAsync(mPhysicsTestScene);
        Physics.autoSimulation = true;
    }
}

 

测试仓库:code

https://gitee.com/Hont/PhysicsSceneDemo对象

相关文章
相关标签/搜索