12-10-2019, 04:35 PM
(10-10-2019, 10:42 AM)josemendez Wrote: Hi,
I'd need to see what your current code is to be able to tell what's wrong.
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());
}
}