-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Add new package Utilis
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## Utilis | ||
|
||
A collection of useful utilities for Unity development. | ||
|
||
|
||
|
||
### General | ||
|
||
1. <u>ReverseNormals</u> | ||
- require MeshFilter component, will reverse mesh normals after start | ||
|
||
|
||
|
||
### Extensions | ||
|
||
1. <u>GetAllChildren</u> | ||
- return all children's transform | ||
- Etc. transform.GetAllChildren | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using UnityEngine; | ||
|
||
public class ReverseNormals : MonoBehaviour | ||
{ | ||
private void Start() | ||
{ | ||
var meshFilter = GetComponent<MeshFilter>(); | ||
if (meshFilter == null) | ||
{ | ||
Debug.LogError("Mesh filter not found!"); | ||
return; | ||
} | ||
|
||
var mesh = meshFilter.mesh; | ||
var normals = mesh.normals; | ||
|
||
// 反转法线方向 | ||
for (var i = 0; i < normals.Length; i++) | ||
{ | ||
normals[i] = -normals[i]; | ||
} | ||
|
||
mesh.normals = normals; | ||
|
||
// 重新计算顶点顺序以保持正确的渲染 | ||
for (var i = 0; i < mesh.subMeshCount; i++) | ||
{ | ||
var triangles = mesh.GetTriangles(i); | ||
for (var j = 0; j < triangles.Length; j += 3) | ||
{ | ||
(triangles[j], triangles[j + 1]) = (triangles[j + 1], triangles[j]); | ||
} | ||
|
||
mesh.SetTriangles(triangles, i); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public static class TransformExtensions | ||
{ | ||
/// <summary> | ||
/// Get all children of a transform | ||
/// </summary> | ||
/// <param name="transform"></param> | ||
/// <returns>List of all children transform</returns> | ||
public static List<Transform> GetAllChildren(this Transform transform) | ||
{ | ||
var children = new List<Transform>(); | ||
GetAllChildrenRecursive(transform, children); | ||
return children; | ||
} | ||
|
||
private static void GetAllChildrenRecursive(Transform transform, List<Transform> children) | ||
{ | ||
foreach (Transform child in transform) | ||
{ | ||
children.Add(child); | ||
GetAllChildrenRecursive(child, children); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "com.hankun.utilis" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "com.hankun.utilis", | ||
"version": "1.0.0", | ||
"displayName": "Utilis", | ||
"description": "A collection of useful utilities for Unity development.", | ||
"unity": "2020.3", | ||
"author": { | ||
"name": "hankun", | ||
"email": "kmcyd1@gmail.com" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.