yaro
yaro
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
,well initially there was a task.run
foreach (var g in objectsToCut)
{
print(g);
Vector3 position = transform.position;
Vector3 originalPos = g.transform.position;
Vector3 originalScale = g.transform.localScale;
Quaternion originalRot = g.transform.rotation;
var mf = g.transform.GetComponent<MeshFilter>();
var mr = g.transform.GetComponent<MeshRenderer>();
var mesh = mf.mesh;
var subMeshCount = mesh.subMeshCount;
var triangles = new List<int[]>();
var vertices = mesh.vertices;
var normals = mesh.normals;
var uv = mesh.uv;
var mats = mr.materials;
for (int i = 0; i < subMeshCount; i++)
{
triangles.Add(mesh.GetTriangles(i));
}
Plane cutPlane = new(g.transform.InverseTransformDirection(cutNormal), g.transform.InverseTransformPoint(position));
var t = Task.Run(() => Cutter.AsyncCut(cutPlane,
originalPos,
originalRot,
originalScale,
mf,
mr,
mesh
, subMeshCount,
triangles,
vertices,
normals,
uv,
mats,
g
));
tasks.Add(t);

}
foreach (var g in objectsToCut)
{
print(g);
Vector3 position = transform.position;
Vector3 originalPos = g.transform.position;
Vector3 originalScale = g.transform.localScale;
Quaternion originalRot = g.transform.rotation;
var mf = g.transform.GetComponent<MeshFilter>();
var mr = g.transform.GetComponent<MeshRenderer>();
var mesh = mf.mesh;
var subMeshCount = mesh.subMeshCount;
var triangles = new List<int[]>();
var vertices = mesh.vertices;
var normals = mesh.normals;
var uv = mesh.uv;
var mats = mr.materials;
for (int i = 0; i < subMeshCount; i++)
{
triangles.Add(mesh.GetTriangles(i));
}
Plane cutPlane = new(g.transform.InverseTransformDirection(cutNormal), g.transform.InverseTransformPoint(position));
var t = Task.Run(() => Cutter.AsyncCut(cutPlane,
originalPos,
originalRot,
originalScale,
mf,
mr,
mesh
, subMeshCount,
triangles,
vertices,
normals,
uv,
mats,
g
));
tasks.Add(t);

}
but on smartphone there is always a lag when I start cutting
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
no io
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
yeah cpu bound
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
no it is my custom made task
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
public static TaskResult AsyncCut(Plane cutPlane,
Vector3 originalPosition,
Quaternion originalRotation,
Vector3 originalScale,
MeshFilter originalMeshFilter, MeshRenderer originalMeshRenderer,
Mesh mesh,
int subMeshCount,
List<int[]> triangles
, Vector3[] vertices
, Vector3[] normals
, Vector2[] uv,
Material[] materials
, GameObject originalObject
)
public static TaskResult AsyncCut(Plane cutPlane,
Vector3 originalPosition,
Quaternion originalRotation,
Vector3 originalScale,
MeshFilter originalMeshFilter, MeshRenderer originalMeshRenderer,
Mesh mesh,
int subMeshCount,
List<int[]> triangles
, Vector3[] vertices
, Vector3[] normals
, Vector2[] uv,
Material[] materials
, GameObject originalObject
)
public class TaskResult
{
public GeneratedMesh leftMesh;
public GeneratedMesh rightMesh;
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
public Material[] materials;
public MeshFilter originalMeshFilter;
public MeshRenderer originalMeshRenderer;
public GameObject originalObject;
public TaskResult(GeneratedMesh leftMesh,
GeneratedMesh rightMesh,
Vector3 position,
Quaternion rotation,
Vector3 scale,
Material[] materials
, MeshFilter originalMeshFilter
, MeshRenderer originalMeshRenderer
, GameObject originalObject
)
{
this.leftMesh = leftMesh;
this.rightMesh = rightMesh;
this.position = position;
this.rotation = rotation;
this.scale = scale;
this.materials = materials;
this.originalMeshFilter = originalMeshFilter;
this.originalMeshRenderer = originalMeshRenderer;
this.originalObject = originalObject;
}
}
public class TaskResult
{
public GeneratedMesh leftMesh;
public GeneratedMesh rightMesh;
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
public Material[] materials;
public MeshFilter originalMeshFilter;
public MeshRenderer originalMeshRenderer;
public GameObject originalObject;
public TaskResult(GeneratedMesh leftMesh,
GeneratedMesh rightMesh,
Vector3 position,
Quaternion rotation,
Vector3 scale,
Material[] materials
, MeshFilter originalMeshFilter
, MeshRenderer originalMeshRenderer
, GameObject originalObject
)
{
this.leftMesh = leftMesh;
this.rightMesh = rightMesh;
this.position = position;
this.rotation = rotation;
this.scale = scale;
this.materials = materials;
this.originalMeshFilter = originalMeshFilter;
this.originalMeshRenderer = originalMeshRenderer;
this.originalObject = originalObject;
}
}
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
did you mean await Parallel.ForEach( cause visual studio doesnt recognise foreachasync
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
i awaited that FooAsync and it didnt help if you are about that
110 replies
CC#
Created by yaro on 8/17/2023 in #help
✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
changing public async void FooAsync() to public async Task FooAsync() didnt fix the issue
110 replies