Search Unity

Create custom physic material

Discussion in 'Physics' started by NotGoodEnoughh, Jul 9, 2019.

  1. NotGoodEnoughh

    NotGoodEnoughh

    Joined:
    Feb 1, 2018
    Posts:
    35
    1) How can I create a physic material with custom properties?
    For example, Unity's physic material has a few properties and I need one more just for holding a value inside.
    upload_2019-7-9_9-55-2.png
    I tried to create CustomPhysicMaterial script but I have a problem with asset creation of this script.
    upload_2019-7-9_9-57-6.png


    When Unity creates its physic material it takes a <.physicMaterial> type.
    2) How can I create a <.physicMaterial> files from CustomPhysicMaterial script?
     
  2. hickv

    hickv

    Joined:
    Oct 31, 2018
    Posts:
    40
    I am after this as well. I got to a point in my game where I would like to add some per physic material properties like footstep sounds and particles etc.

    I am obviously using a scriptable object approach as of now, but I dont like it. It forces me to use a "Collidable" component on gameObjects that have a Collider just so I can reference this scriptable object. I could totally get rid of this component if I could inject my own data/fields in the PhysicMaterial asset instead...

    It doesnt seem that complicated to implement, apparently PhysicMaterial is just a YAML file like ScriptableObject's. I came up with this code setup, but it doens't work, the Editor doesn't serialize the field.

    Code (CSharp):
    1.     public class CustomPhysicMaterial : PhysicMaterial
    2.     {
    3.         public AudioClip footstepSound;
    4.  
    5. #if UNITY_EDITOR
    6.         [UnityEditor.MenuItem("Assets/Create/hickV/CustomPhysicMaterial")]
    7.         public static void CreateAsset()
    8.         {
    9.             CustomPhysicMaterial cPM = new CustomPhysicMaterial();
    10.  
    11.             string path = UnityEditor.AssetDatabase.GetAssetPath(UnityEditor.Selection.activeObject);
    12.  
    13.             if (path == "")
    14.             {
    15.                 path = "Assets";
    16.             }
    17.             else if (Path.GetExtension(path) != "")
    18.             {
    19.                 path = path.Replace(Path.GetFileName(UnityEditor.AssetDatabase.GetAssetPath(UnityEditor.Selection.activeObject)), "");
    20.             }
    21.             UnityEditor.AssetDatabase.CreateAsset(cPM, UnityEditor.AssetDatabase.GenerateUniqueAssetPath(path + "/CustomPhysicMaterial.physicMaterial"));
    22.         }
    23. #endif
    24.     }