2026-02-05 05:07:55 -08:00
using UnityEngine ;
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine.UI ;
using System.IO ;
using UnityEngine.EventSystems ;
#if UNITY_EDITOR
using UnityEditor ;
public class addSliceSystemToCharacterCreatorEditor : EditorWindow
{
2026-03-29 23:03:14 -07:00
GUISkin guiSkin ;
Rect windowRect = new Rect ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
Vector2 rectSize = new Vector2 ( 550 , 600 ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
bool sliceSystemAdded ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
float timeToBuild = 0.2f ;
float timer ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUIStyle style = new GUIStyle ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
float windowHeightPercentage = 0.5f ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
Vector2 screenResolution ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
playerController currentPlayerController ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
bool characterSelected ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
string prefabsPath = "" ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
string sliceRagdollName = "Ragdoll (With Slice System)" ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
public GameObject currentRagdollPrefab ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
public Material sliceMaterial ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
public GameObject characterMeshForRagdollPrefab ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
public bool setTagOnSkeletonRigidbodies = true ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
public string tagOnSkeletonRigidbodies = "box" ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
bool ragdollPrefabCreated ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
float maxLayoutWidht = 250 ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
[MenuItem ("Game Kit Controller/Add Slice System To Character", false, 24)]
public static void addSliceSystemToCharacter ( )
{
GetWindow < addSliceSystemToCharacterCreatorEditor > ( ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
void OnEnable ( )
{
screenResolution = new Vector2 ( Screen . currentResolution . width , Screen . currentResolution . height ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
// Debug.Log (screenResolution + " " + partsHeight + " " + settingsHeight + " " + previewHeight);
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
float totalHeight = screenResolution . y * windowHeightPercentage ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( totalHeight < 400 ) {
totalHeight = 400 ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
rectSize = new Vector2 ( 550 , totalHeight ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
resetCreatorValues ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
prefabsPath = pathInfoValues . getSliceObjectsPrefabsPath ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
checkCurrentCharacterSelected ( Selection . activeGameObject ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
void checkCurrentCharacterSelected ( GameObject currentCharacterSelected )
{
if ( currentCharacterSelected ) {
if ( ! Directory . Exists ( prefabsPath ) ) {
Debug . Log ( "WARNING: " + prefabsPath + " path doesn't exist, make sure the path is from an existing folder in the project" ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
return ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
string pathForRagdoll = prefabsPath + "/" + sliceRagdollName + ".prefab" ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentRagdollPrefab = UnityEditor . AssetDatabase . LoadAssetAtPath ( pathForRagdoll , typeof ( GameObject ) ) as GameObject ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentPlayerController = currentCharacterSelected . GetComponentInChildren < playerController > ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( currentPlayerController ! = null ) {
Debug . Log ( "Character Selected on creator opened" ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
characterSelected = true ;
} else {
characterSelected = false ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
Debug . Log ( "No Character Selected on creator opened" ) ;
}
}
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
void OnDisable ( )
{
resetCreatorValues ( ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
void resetCreatorValues ( )
{
currentPlayerController = null ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
characterSelected = false ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
sliceSystemAdded = false ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
ragdollPrefabCreated = false ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
Debug . Log ( "Slice System window closed" ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
void OnGUI ( )
{
if ( ! guiSkin ) {
guiSkin = Resources . Load ( "GUI" ) as GUISkin ;
}
GUI . skin = guiSkin ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
this . minSize = rectSize ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
this . titleContent = new GUIContent ( "Slice System" , null , "Add Slice System To Character" ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . BeginVertical ( "Add Slice System Window" , "window" ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
EditorGUILayout . Space ( ) ;
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
windowRect = GUILayoutUtility . GetLastRect ( ) ;
// windowRect.position = new Vector2 (0, windowRect.position.y);
windowRect . width = this . maxSize . x ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( ! characterSelected ) {
GUILayout . BeginHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . HelpBox ( "" , MessageType . Info ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
style = new GUIStyle ( EditorStyles . helpBox ) ;
style . richText = true ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
style . fontStyle = FontStyle . Bold ;
style . fontSize = 15 ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . LabelField ( "If not character is selected in the hierarchy, select one and press the button 'Check Current Object Selected'.\n\n" , style ) ;
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . BeginHorizontal ( ) ;
EditorGUILayout . HelpBox ( "" , MessageType . Warning ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
style = new GUIStyle ( EditorStyles . helpBox ) ;
style . richText = true ;
style . fontStyle = FontStyle . Bold ;
style . fontSize = 15 ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . LabelField ( "WARNING: No Character was found, make sure to select the player or an " +
"humanoid AI to add the slice system to it." , style ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . FlexibleSpace ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( GUILayout . Button ( "Check Current Character Selected" ) ) {
checkCurrentCharacterSelected ( Selection . activeGameObject ) ;
}
} else {
GUILayout . BeginHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . HelpBox ( "" , MessageType . Info ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
style = new GUIStyle ( EditorStyles . helpBox ) ;
style . richText = true ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
style . fontStyle = FontStyle . Bold ;
style . fontSize = 15 ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . LabelField ( "You can create the ragdoll prefab for this character, which will be used" +
" when each slice is applied, to instantiate a new ragdoll with those settings and mesh.\n\n" +
"Just select a prefab character mesh on the field 'Character Mesh For Ragdoll' and press the button 'Create Ragdoll Prefab'.\n\n" +
"By default, there is already one selected, but you may want to create a new ragdoll prefab if you are using a new character mesh. " +
"IMPORTANT: Enable the read/write option on the humanoid model setting prefab on unity prefab model window." , style ) ;
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . FlexibleSpace ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . BeginHorizontal ( ) ;
GUILayout . Label ( "Ragdoll Prefab" , EditorStyles . boldLabel , GUILayout . MaxWidth ( maxLayoutWidht ) ) ;
currentRagdollPrefab = EditorGUILayout . ObjectField ( currentRagdollPrefab ,
typeof ( GameObject ) , true , GUILayout . ExpandWidth ( true ) ) as GameObject ;
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . BeginHorizontal ( ) ;
GUILayout . Label ( "Slice Material" , EditorStyles . boldLabel , GUILayout . MaxWidth ( maxLayoutWidht ) ) ;
sliceMaterial = EditorGUILayout . ObjectField ( sliceMaterial ,
typeof ( Material ) , true , GUILayout . ExpandWidth ( true ) ) as Material ;
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . BeginHorizontal ( ) ;
GUILayout . Label ( "Character Mesh For Ragdoll Prefab" , EditorStyles . boldLabel , GUILayout . MaxWidth ( maxLayoutWidht ) ) ;
characterMeshForRagdollPrefab = EditorGUILayout . ObjectField ( characterMeshForRagdollPrefab ,
typeof ( GameObject ) , true , GUILayout . ExpandWidth ( true ) ) as GameObject ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . BeginHorizontal ( ) ;
GUILayout . Label ( "Set Tag On Skeleton" , EditorStyles . boldLabel , GUILayout . MaxWidth ( maxLayoutWidht ) ) ;
setTagOnSkeletonRigidbodies = EditorGUILayout . Toggle ( setTagOnSkeletonRigidbodies ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( setTagOnSkeletonRigidbodies ) {
GUILayout . BeginHorizontal ( ) ;
GUILayout . Label ( "Tag On Skeleton" , EditorStyles . boldLabel , GUILayout . MaxWidth ( maxLayoutWidht ) ) ;
tagOnSkeletonRigidbodies = EditorGUILayout . TextField ( tagOnSkeletonRigidbodies , GUILayout . ExpandWidth ( true ) ) ;
GUILayout . EndHorizontal ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
EditorGUILayout . Space ( ) ;
EditorGUILayout . Space ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( characterMeshForRagdollPrefab ! = null & & ! ragdollPrefabCreated ) {
if ( GUILayout . Button ( "Create Ragdoll Prefab" ) ) {
createRagdollPrefab ( ) ;
}
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( GUILayout . Button ( "Add Slice System To Character" ) ) {
addSliceSystem ( ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( GUILayout . Button ( "Cancel" ) ) {
this . Close ( ) ;
}
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GUILayout . EndVertical ( ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
void addSliceSystem ( )
{
if ( currentRagdollPrefab ) {
GameObject playerControllerGameObject = currentPlayerController . gameObject ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
surfaceToSlice currentSurfaceToSlice = playerControllerGameObject . GetComponent < surfaceToSlice > ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( currentSurfaceToSlice = = null | | currentSurfaceToSlice . getMainSimpleSliceSystem ( ) = = null ) {
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( currentSurfaceToSlice = = null ) {
currentSurfaceToSlice = playerControllerGameObject . AddComponent < surfaceToSlice > ( ) ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GameObject characterMesh = currentPlayerController . getCharacterMeshGameObject ( ) . transform . parent . gameObject ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
playerComponentsManager mainPlayerComponentsManager = currentPlayerController . GetComponent < playerComponentsManager > ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
Transform characterCOMChild = null ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( mainPlayerComponentsManager ! = null ) {
Transform mainCOMTransform = mainPlayerComponentsManager . getIKSystem ( ) . getIKBodyCOM ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( mainCOMTransform ! = null & & mainCOMTransform . childCount > 0 ) {
characterCOMChild = mainCOMTransform . GetChild ( 0 ) ;
}
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
if ( characterCOMChild ! = null & & characterCOMChild . gameObject ! = characterMesh ) {
characterMesh = characterCOMChild . gameObject ;
}
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
simpleSliceSystem currentSimpleSliceSystem = characterMesh . AddComponent < simpleSliceSystem > ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentSimpleSliceSystem . searchBodyParts ( ) ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentSimpleSliceSystem . mainSurfaceToSlice = currentSurfaceToSlice ;
currentSimpleSliceSystem . objectToSlice = characterMesh ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentSurfaceToSlice . setMainSimpleSliceSystem ( currentSimpleSliceSystem . gameObject ) ;
currentSurfaceToSlice . objectIsCharacter = true ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentSurfaceToSlice . mainSurfaceToSlice = characterMesh ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentSimpleSliceSystem . alternatePrefab = currentRagdollPrefab ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
currentSimpleSliceSystem . infillMaterial = sliceMaterial ;
2026-02-05 05:07:55 -08:00
2026-03-29 23:03:14 -07:00
GKC_Utils . updateComponent ( currentSimpleSliceSystem ) ;
GKC_Utils . updateDirtyScene ( "Set slice system info" , currentSimpleSliceSystem . gameObject ) ;
Debug . Log ( "Slice System added to character" ) ;
GKC_Utils . updateDirtyScene ( "Add Slice To Character" , characterMesh ) ;
} else {
Debug . Log ( "Slice System was already configured in this character" ) ;
}
sliceSystemAdded = true ;
} else {
Debug . Log ( "WARNING: no prefab for ragdoll found on path " + prefabsPath + "/" + sliceRagdollName ) ;
}
}
void createRagdollPrefab ( )
{
currentRagdollPrefab = GKC_Utils . createSliceRagdollPrefab ( characterMeshForRagdollPrefab , prefabsPath , sliceMaterial , setTagOnSkeletonRigidbodies , tagOnSkeletonRigidbodies ) ;
ragdollPrefabCreated = currentRagdollPrefab ! = null ;
}
void Update ( )
{
if ( sliceSystemAdded ) {
if ( timer < timeToBuild ) {
timer + = 0.01f ;
if ( timer > timeToBuild ) {
timer = 0 ;
this . Close ( ) ;
}
}
}
}
2026-02-05 05:07:55 -08:00
}
#endif