_xiaofang/xiaofang/Assets/Obi/Scripts/Common/Collisions/ObiCollisionMaterial.cs
杨号敬 bcc74f0465 add
2024-12-18 02:18:45 +08:00

80 lines
2.4 KiB
C#

using UnityEngine;
using System;
using System.Collections;
namespace Obi{
/**
* Holds information about the physics properties of a particle or collider, and how it should react to collisions.
*/
[CreateAssetMenu(fileName = "collision material", menuName = "Obi/Collision Material", order = 180)]
public class ObiCollisionMaterial : ScriptableObject
{
protected ObiCollisionMaterialHandle materialHandle;
public float dynamicFriction;
public float staticFriction;
public float stickiness;
public float stickDistance;
public Oni.MaterialCombineMode frictionCombine;
public Oni.MaterialCombineMode stickinessCombine;
[Space]
public bool rollingContacts = false;
[Indent()]
[VisibleIf("rollingContacts")]
public float rollingFriction;
public ObiCollisionMaterialHandle handle
{
get
{
CreateMaterialIfNeeded();
return materialHandle;
}
}
private void OnEnable()
{
UpdateMaterial();
}
private void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyCollisionMaterial(materialHandle);
}
private void OnValidate()
{
// we can't create GameObjects in OnValidate(), so make sure the colliderworld already exists.
UpdateMaterial();
}
public void UpdateMaterial()
{
var world = ObiColliderWorld.GetInstance();
var mat = world.collisionMaterials[handle.index];
mat.FromObiCollisionMaterial(this);
world.collisionMaterials[handle.index] = mat;
}
protected void CreateMaterialIfNeeded()
{
if (materialHandle == null || !materialHandle.isValid)
{
var world = ObiColliderWorld.GetInstance();
// create the material:
materialHandle = world.CreateCollisionMaterial();
materialHandle.owner = this;
// copy material data from this material (use materialHandle instead of handle, to not retrigger CreateMaterialIfNeeded)
var mat = world.collisionMaterials[materialHandle.index];
mat.FromObiCollisionMaterial(this);
world.collisionMaterials[materialHandle.index] = mat;
}
}
}
}