https://drive.google.com/file/d/0ByVfkx ... zz4-D9BNoA
Hi,
I was a bit disappointed how the .fbx Files exported from Moho appear in Unity.
The provided import script from the extras folder makes it impossible to place characters in a 3D-Scene and if several .fbx files are imported they intersect randomly. So I improved the import script today by mapping the layer order of Moho to the sortingOrder inside Unity's sortingLayers. (The original script manipulates the drawing order of the materials).
To correct the drawing order in a 3D Scene based on the distance of the characters to the camera I wrote another script that has to be applied to the characters in the scene. Now it is possible to place characters in front or behind each other. This script also corrects the drawing of switch layers. It just switches on the first layer of every switch layer and turns off the others. I found that almost always no switch layer is visible at all in Moho's .fbx exports( inside the .fbx's animations they still work good.)
The script corrects the drawing order for the scene view in edit mode, and switches to correct the game view in play mode.
I don't know if it is possible to correct both at the same time.
Since I'm no programmer and these are my first c#-Scripts please suggest better ways of doing things.
Import script for Editor folder:
https://drive.google.com/open?id=0ByVfk ... V82SlJfTU0
script to apply to Moho .fbx gameObjects:
https://drive.google.com/open?id=0ByVfk ... E9velliZ1k
have fun!
MohoFBXImporter.cs
Code: Select all
using UnityEngine;
using UnityEditor;
using System;
// place in Editor folder
public class AnimeStudioPostProcessor : AssetPostprocessor
{
private bool fIsAnimeStudioModel = false;
void OnPreprocessModel()
{
fIsAnimeStudioModel = false;
// resampleRotations only became part of Unity as of version 5.3.
// If you're using an older version of Unity, comment out the following block of code.
// Set resampleRotations to false to fix the "bouncy" handling of constant interpolation keyframes.
try
{
var importer = assetImporter as ModelImporter;
importer.resampleRotations = false;
}
catch
{
}
}
void OnPostprocessGameObjectWithUserProperties(GameObject g, string[] names, System.Object[] values)
{
// Only operate on FBX files
if (assetPath.IndexOf(".fbx") == -1)
{
return;
}
for (int i = 0; i < names.Length; i++)
{
if (names[i] == "ASP_FBX")
{
fIsAnimeStudioModel = true; // at least some part of this comes from Anime Studio
break;
}
}
}
void OnPostprocessModel(GameObject g)
{
// Only operate on FBX files
if (assetPath.IndexOf(".fbx") == -1)
{
return;
}
if (!fIsAnimeStudioModel)
{
//Debug.Log("*** Not Moho ***");
return;
}
Shader shader = Shader.Find("Sprites/Default");
if (shader == null)
return;
Renderer[] renderers = g.GetComponentsInChildren<Renderer>();
foreach (Renderer r in renderers)
{
int renderOrder = 0;
if (r.name.Contains("|"))
{
string[] stringSeparators = new string[] {"|"};
string[] parts = r.name.Split(stringSeparators, StringSplitOptions.None);
int j;
if (Int32.TryParse(parts[parts.Length - 1], out j))
renderOrder = j; //was += j
}
r.sharedMaterial.shader = shader; // apply an unlit shader
/* use sortingOrder on the default Layer instead of r.sharedMaterial.renderQueue
* sortingLayers are used not only by the Sprite Renderer:
*/
r.sortingOrder = renderOrder;
}
}
}
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[ExecuteInEditMode]
// apply to Moho .fbx GameObjects in the scene
public class MohoFBX : MonoBehaviour {
private Vector3 cam ;
private Renderer[] allRs;
public bool SceneViewCam = true;
// Use this for initialization
void Start () {
// show the first layer of every switching layer
if (Application.isEditor == true) {
Transform[] allT = GetComponentsInChildren<Transform> ();
foreach (Transform t in allT) {
if (t.childCount > 0) {
Transform child = t.GetComponentInChildren<Transform> ().GetChild (0);
if (child.localScale == new Vector3 (0.000001f, 0.000001f, 0.000001f)) {
child.transform.localScale = new Vector3 (1f, 1f, 1f);
// don't show the other layers
for (int i = 1; i < t.childCount; i++) {
Transform otherChild = t.GetComponentInChildren<Transform> ().GetChild (i);
otherChild.transform.localScale = new Vector3 (0.000001f, 0.000001f, 0.000001f);
}
}
}
}
}
}
// Update is called once per frame
void Update () {
// use main Camera to calculate drawing order
cam = Camera.main.transform.position;
// this corrects drawing the Scene view if not playing:
if (Camera.current != null && Application.isPlaying == false && SceneViewCam == true) {
cam = Camera.current.transform.position;
}
// draw in layers in order but also use distance from camera
float dist2cam = Vector3.Distance (cam, transform.position) * -100;
Renderer[] allRs = GetComponentsInChildren<Renderer> ();
foreach (Renderer r in allRs) {
int renderOrder = 0;
if (r.name.Contains("|"))
{
string[] stringSeparators = new string[] {"|"};
string[] parts = r.name.Split(stringSeparators, StringSplitOptions.None);
int j;
if (Int32.TryParse(parts[parts.Length - 1], out j))
renderOrder = j + (int)dist2cam;
}
r.GetComponent<Renderer> ().sortingOrder = renderOrder;
}
}
}
And here is an additional optional Editor script that applies changing the drawing mode (game view / scene view) in one MohoFBX object to all others in the scene automatically:
MohoFBXInspector.cs
https://drive.google.com/open?id=0ByVfk ... nY4Vnh4b1U
Code: Select all
using UnityEditor;
using UnityEngine;
using System.Collections;
// place this script in Editor folder
// move an object to update if it does not redraw by itself instantly
// Custom Editor using SerializedProperties.
// Automatic handling of multi-object editing, undo, and prefab overrides.
[CustomEditor(typeof(MohoFBX))]
[CanEditMultipleObjects] // not really needed here
public class updater : Editor {
SerializedProperty useAllSceneViews;
void OnEnable () {
// Setup the SerializedProperties.
useAllSceneViews = serializedObject.FindProperty ("SceneViewCam");
}
public override void OnInspectorGUI() {
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update ();
// Show the custom GUI controls.
EditorGUILayout.PropertyField (useAllSceneViews, new GUIContent("Game / Scene View") );
// find all MohoFBX objects and change their drawing mode, too:
MohoFBX[] mohoFBXs = (MohoFBX[])GameObject.FindObjectsOfType (typeof(MohoFBX));
foreach (MohoFBX m in mohoFBXs) {
m.SceneViewCam = useAllSceneViews.boolValue;
}
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties ();
}
}