You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
822 B

  1. using UnityEngine;
  2. public class SimpleGPUInstancingExample : MonoBehaviour
  3. {
  4. public Transform Prefab;
  5. public Material InstancedMaterial;
  6. void Awake()
  7. {
  8. #if UNITY_5_6_OR_NEWER
  9. InstancedMaterial.enableInstancing = true;
  10. #endif
  11. int range = 5;
  12. for ( int i = 0; i < 1000; i++ )
  13. {
  14. Transform newInstance = Instantiate( Prefab, new Vector3( Random.Range( -range, range ), range + Random.Range( -range, range ), Random.Range( -range, range ) ), Quaternion.identity ) as Transform;
  15. MaterialPropertyBlock matpropertyBlock = new MaterialPropertyBlock();
  16. Color newColor = new Color( Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ) );
  17. matpropertyBlock.SetColor( "_Color", newColor );
  18. newInstance.GetComponent<MeshRenderer>().SetPropertyBlock( matpropertyBlock );
  19. }
  20. }
  21. }