Unity PhysicsScene测试

应该是unity 2018.3中加入的功能,对象可以放置于不同的物理场景中。

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

 

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

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;
    }
}

 

测试仓库:

https://gitee.com/Hont/PhysicsSceneDemo

posted @ 2019-06-02 09:33  HONT  阅读(1656)  评论(0编辑  收藏  举报