ObjectUtility.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using UnityEngine;
  5. namespace MagicaCloth
  6. {
  7. /// <summary>
  8. /// オブジェクト生成ユーティリティ
  9. /// 主にデバッグ用
  10. /// </summary>
  11. public static class ObjectUtility
  12. {
  13. /// <summary>
  14. /// 範囲内に散らばったキューブを作成する
  15. /// </summary>
  16. /// <param name="count"></param>
  17. /// <param name="radius"></param>
  18. /// <returns></returns>
  19. public static GameObject[] PlaceRandomCubes(int count, Vector3 center, float radius)
  20. {
  21. var cubes = new GameObject[count];
  22. var cubeToCopy = MakeStrippedCube();
  23. for (int i = 0; i < count; i++)
  24. {
  25. var cube = GameObject.Instantiate(cubeToCopy);
  26. cube.transform.position = center + Random.insideUnitSphere * radius;
  27. cubes[i] = cube;
  28. }
  29. GameObject.Destroy(cubeToCopy);
  30. return cubes;
  31. }
  32. /// <summary>
  33. /// キューブを作成する
  34. /// </summary>
  35. /// <param name="count"></param>
  36. /// <returns></returns>
  37. public static GameObject[] PlaceRandomCubes(int count)
  38. {
  39. return PlaceRandomCubes(count, Vector3.zero, 0.0f);
  40. }
  41. /// <summary>
  42. /// 影を落とさず当たり判定を取らないキューブを1つ作成する
  43. /// </summary>
  44. /// <returns></returns>
  45. public static GameObject MakeStrippedCube()
  46. {
  47. var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  48. //turn off shadows entirely
  49. var renderer = cube.GetComponent<MeshRenderer>();
  50. renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  51. renderer.receiveShadows = false;
  52. // disable collision
  53. var collider = cube.GetComponent<Collider>();
  54. collider.enabled = false;
  55. return cube;
  56. }
  57. public static GameObject[] PlaceRandomGameObject(int count, Vector3 center, float radius, GameObject prefab)
  58. {
  59. var objs = new GameObject[count];
  60. for (int i = 0; i < count; i++)
  61. {
  62. var obj = GameObject.Instantiate(prefab);
  63. obj.transform.position = center + Random.insideUnitSphere * radius;
  64. objs[i] = obj;
  65. }
  66. return objs;
  67. }
  68. }
  69. }