148 lines
4.1 KiB
C#
148 lines
4.1 KiB
C#
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
|
|
|
using System;
|
|
using UnityEngine;
|
|
using FairyGUI;
|
|
using NBC;
|
|
|
|
namespace NBF
|
|
{
|
|
public partial class ModelTexture : GComponent
|
|
{
|
|
ModelRenderImage _renderImage;
|
|
private Vector2 _startPos;
|
|
private Vector3 _startRot;
|
|
private SwipeGesture _swipeGesture;
|
|
private bool _focus;
|
|
|
|
|
|
private void OnInited()
|
|
{
|
|
SetRotateListening();
|
|
// Stage.inst.on
|
|
TouchHolder.onRollOver.Set(OnFocusIn);
|
|
TouchHolder.onRollOut.Set(OnFocusOut);
|
|
Stage.inst.onMouseWheel.Add(OnMouseWheel);
|
|
// TouchHolder.onm
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
Stage.inst.onMouseWheel.Remove(OnMouseWheel);
|
|
base.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载资产模型
|
|
/// </summary>
|
|
/// <param name="configId"></param>
|
|
public void LoadAsset(int configId)
|
|
{
|
|
if (_renderImage == null)
|
|
{
|
|
_renderImage = new ModelRenderImage(ModelHolder.asGraph);
|
|
}
|
|
|
|
var model = _renderImage.LoadModel("Role/npc");
|
|
_renderImage.modelRoot.localScale = Vector3.one;
|
|
|
|
var previewableAsset = model.GetComponent<PreviewableAsset>();
|
|
if (previewableAsset != null)
|
|
{
|
|
_renderImage.modelRoot.localPosition = new Vector3(0, 0, previewableAsset.zoom.zoom);
|
|
_renderImage.modelRoot.localRotation = Quaternion.Euler(previewableAsset.rotation);
|
|
}
|
|
else
|
|
{
|
|
_renderImage.modelRoot.localPosition = new Vector3(0, 0, 3);
|
|
_renderImage.modelRoot.localRotation = Quaternion.Euler(Vector3.zero);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载角色模型
|
|
/// </summary>
|
|
public void LoadRole()
|
|
{
|
|
}
|
|
|
|
private void SetRotateListening()
|
|
{
|
|
var dragObj = TouchHolder;
|
|
var gesture1 = new SwipeGesture(dragObj);
|
|
gesture1.onMove.Set(OnSwipeMove);
|
|
gesture1.onEnd.Set(OnSwipeEnd);
|
|
}
|
|
|
|
#region 旋转和平移
|
|
|
|
private void OnSwipeMove(EventContext context)
|
|
{
|
|
var gesture = context.sender as SwipeGesture;
|
|
if (gesture == null) return;
|
|
var v = Vector3.zero;
|
|
|
|
|
|
if (context.inputEvent.button == 0)
|
|
{
|
|
v.y = -gesture.delta.x * 0.2f;
|
|
v.z = -gesture.delta.y * 0.2f;
|
|
if (!gesture.snapping)
|
|
{
|
|
v.y = 0;
|
|
v.z = 0;
|
|
}
|
|
|
|
if (Mathf.Abs(v.y) < 1) //消除手抖影响
|
|
v.y = 0;
|
|
if (Mathf.Abs(v.z) < 1) //消除手抖影响
|
|
v.z = 0;
|
|
_renderImage.StartRotate(v);
|
|
}
|
|
else
|
|
{
|
|
v.y = -gesture.delta.y * 0.005f;
|
|
v.x = gesture.delta.x * 0.005f;
|
|
//平移
|
|
var pos = _renderImage.modelRoot.localPosition;
|
|
_renderImage.modelRoot.localPosition = new Vector3(pos.x + v.x, pos.y + v.y, pos.z);
|
|
}
|
|
}
|
|
|
|
private void OnSwipeEnd(EventContext context)
|
|
{
|
|
_renderImage.StopRotate();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Zoom
|
|
|
|
private void OnFocusIn()
|
|
{
|
|
_focus = true;
|
|
Log.Info("focus true");
|
|
}
|
|
|
|
private void OnFocusOut()
|
|
{
|
|
_focus = false;
|
|
Log.Info("focus false");
|
|
}
|
|
|
|
private void OnMouseWheel(EventContext context)
|
|
{
|
|
if (!_focus) return;
|
|
float delta = context.inputEvent.mouseWheelDelta / Stage.devicePixelRatio;
|
|
SetZoom(delta * 0.2f);
|
|
}
|
|
|
|
private void SetZoom(float delta)
|
|
{
|
|
var pos = _renderImage.modelRoot.localPosition;
|
|
_renderImage.modelRoot.localPosition = new Vector3(pos.x, pos.y, pos.z + delta);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |