Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  About the cloth generation script before execution
#4
(12-10-2019, 04:35 PM)tukuyo Wrote: Thank you for your quick reply.

The script is attached below.

The first is the Editor script.

The second is the part of the script that is run from the Editor script.

The third is a script attached to the generated Cloth object.

(In some places, comment out and Japanese is written, but don't worry.)

Best regards,



Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Master))]
public class GenerateCloth : Editor
{
   public override void OnInspectorGUI()
   {
       DrawDefaultInspector();

       Master script = (Master)target;
       if(!EditorApplication.isPlaying){
           if (GUILayout.Button("Genarete!!")){
               script.CreateCloth();
           }
       }
   }

}



Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;
using Obi;

public class Master : MonoBehaviour {

   // 布オブジェクトを持つ親
   [SerializeField]
   private GameObject testParent;

   [Header("=====基本布生成=====")]
   // 生成する布の大きさm と分割数
   [SerializeField]
   private float _scale = 1;
   [SerializeField]
   private int _resolution = 10;
   public Material outsideMaterial;
   public Material insideMaterial;
   
   public ObiSolver solver;
   public ObiMeshTopology topology;

   public bool useObi = false;


   public void CreateCloth()
    {
       if(!useObi){
              // Processing when Obi is not used
       } else {
           int num = obisettings.Length;
           if(num <= 0) return;
           // テスト分オブジェクト生成
           for (int i = 0; i < num; i++)
           {
               Mesh mesh = createMesh(_scale, _resolution);

               GameObject sheet = new GameObject("Sheet" + i, typeof(MeshFilter), typeof(SkinnedMeshRenderer));
               sheet.GetComponent<SkinnedMeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };
               sheet.transform.parent = testParent.transform;
               sheet.transform.localPosition = Vector3.forward;
               sheet.tag = "sheet";
               sheet.AddComponent<ObiCloth>();
               genCloth generator = sheet.AddComponent<genCloth>();
               generator.mesh = mesh;
               generator.resolution = _resolution;
               generator.topology = topology;
               generator.solver = solver;
               generator.setUpObi();
           }
       }
   }
}



Code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(MeshFilter))]
//[RequireComponent (typeof(MeshRenderer))]
[RequireComponent(typeof(SkinnedMeshRenderer))]

public class genCloth : MonoBehaviour
{

   public Mesh mesh;
   public SphereCollider virtualHand;
   public float Damping = 0;
   public float Stretching = 0;
   public float Bending = 0;
   public int resolution;
   public Vector3 Accelerration;
   private Cloth component;

   public ObiSolver solver;                /**< solver to add the sack to.*/
    public ObiMeshTopology topology;

   public void Awake(){
       if(this.gameObject.GetComponent<ObiCloth>()){
           ObiCloth cloth = GetComponent<ObiCloth>();
           CoroutineJob job = new CoroutineJob();
           job.Start(cloth.GeneratePhysicRepresentationForMesh());
       }
   }

public void setUpObi() {
       if(mesh == null || topology == null){
           Debug.LogError("Either the mesh or the topology are null. You must provide a mesh and an empty topology asset in order to generate cloth.");
       }

       ObiCloth cloth = GetComponent<ObiCloth>();

       var filter = GetComponent<MeshFilter>();
       filter.sharedMesh = mesh;
       var skin = GetComponent<SkinnedMeshRenderer>();
       skin.sharedMesh = mesh;

       topology.InputMesh = mesh;
       topology.Generate();

       cloth.Solver = solver;
       cloth.SharedTopology = topology;

       cloth.enabled = true;
       cloth.AddToSolver(null);

       CoroutineJob job = new CoroutineJob();
        job.Start(cloth.GeneratePhysicRepresentationForMesh());

   }

}
Hi.

cloth.GeneratePhysicRepresentationForMesh is a coroutine, and CoroutineJob() is meant to execute coroutines in the editor. Use StartCoroutine instead. See:
https://docs.unity3d.com/ScriptReference...utine.html
Reply


Messages In This Thread
RE: About the cloth generation script before execution - by josemendez - 12-10-2019, 05:12 PM