Skip to content

Commit

Permalink
[unity] Handle PreviewUtility API in Unity2017.1 (#961)
Browse files Browse the repository at this point in the history
* Minor fixes for Unity2017
* [unity] Handle PreviewUtility API in Unity2017.1
  • Loading branch information
moritz-wundke authored and pharan committed Aug 3, 2017
1 parent 1b71da6 commit 5d8b1b4
Showing 1 changed file with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ void RepopulateWarnings () {
bool m_requireRefresh;
Color m_originColor = new Color(0.3f, 0.3f, 0.3f, 1);

Camera PreviewUtilityCamera {
get {
#if UNITY_2017_1_OR_NEWER
return m_previewUtility.camera;
#else
return m_previewUtility.m_Camera;
#endif
}
}

void StopAnimation () {
if (m_skeletonAnimation == null) {
Debug.LogWarning("Animation was stopped but preview doesn't exist. It's possible that the Preview Panel is closed.");
Expand Down Expand Up @@ -577,7 +587,7 @@ void InitPreview () {
if (this.m_previewUtility == null) {
this.m_lastTime = Time.realtimeSinceStartup;
this.m_previewUtility = new PreviewRenderUtility(true);
var c = this.m_previewUtility.m_Camera;
var c = this.PreviewUtilityCamera;
c.orthographic = true;
c.orthographicSize = 1;
c.cullingMask = -2147483648;
Expand Down Expand Up @@ -700,16 +710,17 @@ void AdjustCamera () {
if (EditorApplication.timeSinceStartup < m_adjustFrameEndTime)
AdjustCameraGoals();

float orthoSet = Mathf.Lerp(this.m_previewUtility.m_Camera.orthographicSize, m_orthoGoal, 0.1f);
var c = this.PreviewUtilityCamera;
float orthoSet = Mathf.Lerp(c.orthographicSize, m_orthoGoal, 0.1f);

this.m_previewUtility.m_Camera.orthographicSize = orthoSet;
c.orthographicSize = orthoSet;

float dist = Vector3.Distance(m_previewUtility.m_Camera.transform.position, m_posGoal);
float dist = Vector3.Distance(c.transform.position, m_posGoal);
if(dist > 0f) {
Vector3 pos = Vector3.Lerp(this.m_previewUtility.m_Camera.transform.position, m_posGoal, 0.1f);
Vector3 pos = Vector3.Lerp(c.transform.position, m_posGoal, 0.1f);
pos.x = 0;
this.m_previewUtility.m_Camera.transform.position = pos;
this.m_previewUtility.m_Camera.transform.rotation = Quaternion.identity;
c.transform.position = pos;
c.transform.rotation = Quaternion.identity;
m_requireRefresh = true;
}
}
Expand All @@ -728,18 +739,20 @@ void DoRenderPreview (bool drawHandles) {
if (!EditorApplication.isPlaying)
m_skeletonAnimation.LateUpdate();

var c = this.PreviewUtilityCamera;

if (drawHandles) {
Handles.SetCamera(m_previewUtility.m_Camera);
Handles.SetCamera(c);
Handles.color = m_originColor;

Handles.DrawLine(new Vector3(-1000 * m_skeletonDataAsset.scale, 0, 0), new Vector3(1000 * m_skeletonDataAsset.scale, 0, 0));
Handles.DrawLine(new Vector3(0, 1000 * m_skeletonDataAsset.scale, 0), new Vector3(0, -1000 * m_skeletonDataAsset.scale, 0));
}

this.m_previewUtility.m_Camera.Render();
c.Render();

if (drawHandles) {
Handles.SetCamera(m_previewUtility.m_Camera);
Handles.SetCamera(c);
SpineHandles.DrawBoundingBoxes(m_skeletonAnimation.transform, m_skeletonAnimation.skeleton);
if (showAttachments) SpineHandles.DrawPaths(m_skeletonAnimation.transform, m_skeletonAnimation.skeleton);
}
Expand Down Expand Up @@ -922,19 +935,19 @@ public override void OnPreviewSettings () {
}
}


public override Texture2D RenderStaticPreview (string assetPath, UnityEngine.Object[] subAssets, int width, int height) {
var tex = new Texture2D(width, height, TextureFormat.ARGB32, false);

this.InitPreview();
if (this.m_previewUtility.m_Camera == null)
var c = this.PreviewUtilityCamera;
if (c == null)
return null;

m_requireRefresh = true;
this.DoRenderPreview(false);
AdjustCameraGoals(false);
this.m_previewUtility.m_Camera.orthographicSize = m_orthoGoal / 2;
this.m_previewUtility.m_Camera.transform.position = m_posGoal;
c.orthographicSize = m_orthoGoal / 2;
c.transform.position = m_posGoal;
this.m_previewUtility.BeginStaticPreview(new Rect(0, 0, width, height));
this.DoRenderPreview(false);
tex = this.m_previewUtility.EndStaticPreview();
Expand Down

0 comments on commit 5d8b1b4

Please sign in to comment.