首页 > 其他分享 >unity 摇杆

unity 摇杆

时间:2024-10-08 17:32:42浏览次数:5  
标签:axisValue Vector2 axisX axisY 摇杆 unity && public

/***********************************************
                EasyTouch Controls
    Copyright © 2016 The Hedgehog Team
      http://www.thehedgehogteam.com/Forum/
        
      [email protected]
        
**********************************************/
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;

[System.Serializable]
public class ETCJoystick : ETCBase,IPointerEnterHandler,IDragHandler, IBeginDragHandler, IPointerDownHandler, IPointerUpHandler {
        
    #region Unity Events
    [System.Serializable] public class OnMoveStartHandler : UnityEvent{}
    [System.Serializable] public class OnMoveSpeedHandler : UnityEvent<Vector2> { }
    [System.Serializable] public class OnMoveHandler : UnityEvent<Vector2> { }
    [System.Serializable] public class OnMoveEndHandler : UnityEvent{ }

    [System.Serializable] public class OnTouchStartHandler : UnityEvent{}
    [System.Serializable] public class OnTouchUpHandler : UnityEvent{ }

    [System.Serializable] public class OnDownUpHandler : UnityEvent{ }
    [System.Serializable] public class OnDownDownHandler : UnityEvent{ }
    [System.Serializable] public class OnDownLeftHandler : UnityEvent{ }
    [System.Serializable] public class OnDownRightHandler : UnityEvent{ }

    [System.Serializable] public class OnPressUpHandler : UnityEvent{ }
    [System.Serializable] public class OnPressDownHandler : UnityEvent{ }
    [System.Serializable] public class OnPressLeftHandler : UnityEvent{ }
    [System.Serializable] public class OnPressRightHandler : UnityEvent{ }


    [SerializeField] public OnMoveStartHandler onMoveStart;
    [SerializeField] public OnMoveHandler onMove;
    [SerializeField] public OnMoveSpeedHandler onMoveSpeed;
    [SerializeField] public OnMoveEndHandler onMoveEnd;

    [SerializeField] public OnTouchStartHandler onTouchStart;
    [SerializeField] public OnTouchUpHandler onTouchUp;

    [SerializeField] public OnDownUpHandler OnDownUp;
    [SerializeField] public OnDownDownHandler OnDownDown;
    [SerializeField] public OnDownLeftHandler OnDownLeft;
    [SerializeField] public OnDownRightHandler OnDownRight;

    [SerializeField] public OnDownUpHandler OnPressUp;
    [SerializeField] public OnDownDownHandler OnPressDown;
    [SerializeField] public OnDownLeftHandler OnPressLeft;
    [SerializeField] public OnDownRightHandler OnPressRight;
    #endregion

    #region Enumeration
    public enum JoystickArea { UserDefined,FullScreen, Left,Right,Top, Bottom, TopLeft, TopRight, BottomLeft, BottomRight};
    public enum JoystickType {Dynamic, Static};
    public enum RadiusBase {Width, Height, UserDefined};
    #endregion

    #region Members

    #region Public members
    public JoystickType joystickType;
    public bool allowJoystickOverTouchPad;
    public RadiusBase radiusBase;
    public float radiusBaseValue;
    public ETCAxis axisX;
    public ETCAxis axisY;
    public RectTransform thumb;
    
    public JoystickArea joystickArea;
    public RectTransform userArea;

    public bool isTurnAndMove = false;
    public float tmSpeed = 10;
    public float tmAdditionnalRotation = 0;
    public AnimationCurve tmMoveCurve;
    public bool tmLockInJump = false;
    private Vector3 tmLastMove;

    #endregion
        
    #region Private members
    private Vector2 thumbPosition;
    private bool isDynamicActif;
    private Vector2 tmpAxis;
    private Vector2 OldTmpAxis;
    private bool isOnTouch;


    #endregion

    #region Joystick behavior option
    [SerializeField]
    private bool isNoReturnThumb;
    public bool IsNoReturnThumb {
        get {
            return isNoReturnThumb;
        }
        set {
            isNoReturnThumb = value;
        }
    }    

    private Vector2 noReturnPosition;
    private Vector2 noReturnOffset;

    [SerializeField]
    private bool isNoOffsetThumb;
    public bool IsNoOffsetThumb {
        get {
            return isNoOffsetThumb;
        }
        set {
            isNoOffsetThumb = value;
        }
    }
    #endregion

    #region Inspector


    #endregion

    #endregion
    
    #region Constructor
    public ETCJoystick(){
        joystickType = JoystickType.Static;
        allowJoystickOverTouchPad = false;
        radiusBase = RadiusBase.Width;

        axisX = new ETCAxis("Horizontal");
        axisY = new ETCAxis("Vertical");
    
        _visible = true;
        _activated = true;

        joystickArea = JoystickArea.FullScreen;

        isDynamicActif = false;
        isOnDrag = false;
        isOnTouch = false;

        axisX.unityAxis = "Horizontal";
        axisY.unityAxis = "Vertical";

        enableKeySimulation = true;

        isNoReturnThumb = false;

        showPSInspector = false;
        showAxesInspector = false;
        showEventInspector = false;
        showSpriteInspector = false;
    }
    #endregion

    #region Monobehaviours Callback
    protected override void Awake (){

        base.Awake ();

        if (joystickType == JoystickType.Dynamic){
            this.rectTransform().anchorMin = new Vector2(0.5f,0.5f);
            this.rectTransform().anchorMax = new Vector2(0.5f,0.5f);
            this.rectTransform().SetAsLastSibling();
            visible = false;
        }

        if (allowSimulationStandalone && enableKeySimulation && !Application.isEditor && joystickType!=JoystickType.Dynamic){
            SetVisible(visibleOnStandalone);
        }
    }

    public override void Start(){
    
        axisX.InitAxis();
        axisY.InitAxis();

        if (enableCamera){
            InitCameraLookAt();
        }

        tmpAxis = Vector2.zero;
        OldTmpAxis = Vector2.zero;

        noReturnPosition = thumb.position;

        pointId = -1;

        if (joystickType == JoystickType.Dynamic){
            visible = false;
        }

        base.Start();

        // Init Camera position
        if (enableCamera && cameraMode == CameraMode.SmoothFollow){
            if (cameraTransform && cameraLookAt){
                cameraTransform.position = cameraLookAt.TransformPoint( new Vector3(0,followHeight,-followDistance));
                cameraTransform.LookAt( cameraLookAt);
            }
        }

        if (enableCamera && cameraMode == CameraMode.Follow){
            if (cameraTransform && cameraLookAt){
                cameraTransform.position = cameraLookAt.position + followOffset;
                cameraTransform.LookAt( cameraLookAt.position);
            }
        }
    }


    public override void Update (){

        base.Update ();

        #region dynamic joystick
        if (joystickType == JoystickType.Dynamic && !_visible && _activated){
            Vector2 localPosition = Vector2.zero;
            Vector2 screenPosition = Vector2.zero;
            
            if (isTouchOverJoystickArea(ref localPosition, ref screenPosition)){
                
                GameObject overGO = GetFirstUIElement( screenPosition);
                
                if (overGO == null || (allowJoystickOverTouchPad && overGO.GetComponent<ETCTouchPad>()) || (overGO != null && overGO.GetComponent<ETCArea>() ) ) {

                    cachedRectTransform.anchoredPosition = localPosition;
                    visible = true;
                }
            }
        }


        if (joystickType == JoystickType.Dynamic && _visible){
            if (GetTouchCount()==0){
                visible = false;

            }
        }
        #endregion

    }

    public override void LateUpdate (){

        if (enableCamera && !cameraLookAt ){
            InitCameraLookAt();
        }
        base.LateUpdate ();

    }

    private void InitCameraLookAt(){

        if (cameraTargetMode == CameraTargetMode.FromDirectActionAxisX){
            cameraLookAt = axisX.directTransform;
        }
        else if (cameraTargetMode == CameraTargetMode.FromDirectActionAxisY){
            cameraLookAt = axisY.directTransform;
            if (isTurnAndMove){
                cameraLookAt = axisX.directTransform;
            }
        }
        else if (cameraTargetMode == CameraTargetMode.LinkOnTag){
            GameObject tmpobj = GameObject.FindGameObjectWithTag(camTargetTag);
            if (tmpobj){
                cameraLookAt = tmpobj.transform;
            }
        }

        if (cameraLookAt)
            cameraLookAtCC = cameraLookAt.GetComponent<CharacterController>();
    }

    protected override void UpdateControlState (){
    
        if (_visible){
            UpdateJoystick();
        }
        else{
            if (joystickType == JoystickType.Dynamic){
                OnUp( false);
            }
        }
    }

    #endregion
    
    #region UI Callback
    public void OnPointerEnter(PointerEventData eventData){

        if (joystickType == JoystickType.Dynamic && !isDynamicActif && _activated &&  pointId==-1){
            eventData.pointerDrag = gameObject;
            eventData.pointerPress = gameObject;

            isDynamicActif = true;
            pointId = eventData.pointerId;
        }

        if (joystickType == JoystickType.Dynamic &&  !eventData.eligibleForClick){
            OnPointerUp( eventData );
        }

    }

    public void OnPointerDown(PointerEventData eventData){
        onTouchStart.Invoke();
        pointId = eventData.pointerId;
        if (isNoOffsetThumb){
            OnDrag( eventData);
        }
    }

    public void OnBeginDrag(PointerEventData eventData){


    }
    
    public void OnDrag(PointerEventData eventData){

        if (pointId == eventData.pointerId){
            isOnDrag = true;
            isOnTouch = true;

            float radius =  GetRadius();


            if (!isNoReturnThumb){
                thumbPosition =  (eventData.position - eventData.pressPosition);// / (cachedRootCanvas.rectTransform().localScale.x  ) ;
            }
            else{
                thumbPosition =((eventData.position - noReturnPosition) /cachedRootCanvas.rectTransform().localScale.x) + noReturnOffset;
            }

            if (isNoOffsetThumb){
                thumbPosition =  (eventData.position - (Vector2)cachedRectTransform.position) / cachedRootCanvas.rectTransform().localScale.x;
            }

            thumbPosition.x = Mathf.FloorToInt( thumbPosition.x);
            thumbPosition.y = Mathf.FloorToInt( thumbPosition.y);


            if (!axisX.enable){
                thumbPosition.x=0;
            }

            if (!axisY.enable){
                thumbPosition.y=0;
            }

            if (thumbPosition.magnitude > radius){
                if (!isNoReturnThumb){
                    thumbPosition = thumbPosition.normalized * radius ;
                }
                else{
                    thumbPosition = thumbPosition.normalized * radius;
                }
            }

            thumb.anchoredPosition =  thumbPosition; 
        }
    }

    public void OnPointerUp (PointerEventData eventData){
        if (pointId == eventData.pointerId){
            OnUp();
        }
    }

    private void OnUp(bool real=true){

        isOnDrag = false;
        isOnTouch = false;
        
        if (isNoReturnThumb){
            noReturnPosition = thumb.position;
            noReturnOffset = thumbPosition;
        }
        
        if (!isNoReturnThumb){
            thumbPosition =  Vector2.zero;
            thumb.anchoredPosition = Vector2.zero;
            
            axisX.axisState = ETCAxis.AxisState.None;
            axisY.axisState = ETCAxis.AxisState.None;
        }
        
        if (!axisX.isEnertia && !axisY.isEnertia){
            axisX.ResetAxis();
            axisY.ResetAxis();
            tmpAxis = Vector2.zero;
            OldTmpAxis = Vector2.zero;
            if (real){
                onMoveEnd.Invoke();
            }
        }
        
        if (joystickType == JoystickType.Dynamic){
            visible = false;
            isDynamicActif = false;
        }

        pointId=-1;

        if (real){
            onTouchUp.Invoke();
        }
    }
    #endregion

    #region Joystick Update
    protected override void DoActionBeforeEndOfFrame (){
        axisX.DoGravity();
        axisY.DoGravity();
    }

    private void UpdateJoystick(){

        #region Unity axes
        //enableKeySimulation && 
        if (!isOnTouch && _activated && _visible ){

            float x = Input.GetAxis(axisX.unityAxis);
            float y= Input.GetAxis(axisY.unityAxis);

            if (!isNoReturnThumb){
                thumb.localPosition = Vector2.zero;
            }

            isOnDrag = false;

            if (x!=0){
                isOnDrag = true;
                thumb.localPosition = new Vector2(GetRadius()*x, thumb.localPosition.y);
            }

            if (y!=0){
                isOnDrag = true;
                thumb.localPosition = new Vector2(thumb.localPosition.x,GetRadius()*y );
            }

            thumbPosition = thumb.localPosition;
        }
        #endregion

        // Computejoystick value
        OldTmpAxis.x = axisX.axisValue;
        OldTmpAxis.y = axisY.axisValue;

        tmpAxis = thumbPosition / GetRadius();

        axisX.UpdateAxis( tmpAxis.x,isOnDrag, ETCBase.ControlType.Joystick,true);
        axisY.UpdateAxis( tmpAxis.y,isOnDrag, ETCBase.ControlType.Joystick,true);

        #region Move event
        if ((axisX.axisValue!=0 ||  axisY.axisValue!=0 ) && OldTmpAxis == Vector2.zero){
            onMoveStart.Invoke();
        }
        if (axisX.axisValue!=0 ||  axisY.axisValue!=0 ){

            if (!isTurnAndMove){
                // X axis
                if( axisX.actionOn == ETCAxis.ActionOn.Down && (axisX.axisState == ETCAxis.AxisState.DownLeft || axisX.axisState == ETCAxis.AxisState.DownRight)){
                    axisX.DoDirectAction();
                }
                else if (axisX.actionOn == ETCAxis.ActionOn.Press){
                    axisX.DoDirectAction();
                }

                // Y axis
                if( axisY.actionOn == ETCAxis.ActionOn.Down && (axisY.axisState == ETCAxis.AxisState.DownUp || axisY.axisState == ETCAxis.AxisState.DownDown)){
                    axisY.DoDirectAction();
                }
                else if (axisY.actionOn == ETCAxis.ActionOn.Press){
                    axisY.DoDirectAction();
                }
            }
            else{
                DoTurnAndMove();
            }
            onMove.Invoke( new Vector2(axisX.axisValue,axisY.axisValue));
            onMoveSpeed.Invoke( new Vector2(axisX.axisSpeedValue,axisY.axisSpeedValue));
        }
        else if (axisX.axisValue==0 &&  axisY.axisValue==0  && OldTmpAxis!=Vector2.zero) {
            onMoveEnd.Invoke();
        }        

        if (!isTurnAndMove){
            if (axisX.axisValue==0 &&  axisX.directCharacterController ){
                if (!axisX.directCharacterController.isGrounded && axisX.isLockinJump)
                    axisX.DoDirectAction();
            } 

            if (axisY.axisValue==0 &&  axisY.directCharacterController ){
                if (!axisY.directCharacterController.isGrounded && axisY.isLockinJump)
                    axisY.DoDirectAction();
            }
        }
        else{
            if ((axisX.axisValue==0 && axisY.axisValue==0) &&  axisX.directCharacterController ){
                if (!axisX.directCharacterController.isGrounded && tmLockInJump)
                    DoTurnAndMove();
            }
        }

        #endregion


        #region Down & press event
        float coef =1;
        if (axisX.invertedAxis) coef = -1;
        if (Mathf.Abs(OldTmpAxis.x)< axisX.axisThreshold && Mathf.Abs(axisX.axisValue)>=axisX.axisThreshold){

            if (axisX.axisValue*coef >0){
                axisX.axisState = ETCAxis.AxisState.DownRight;
                OnDownRight.Invoke();
            }
            else if (axisX.axisValue*coef<0){
                axisX.axisState = ETCAxis.AxisState.DownLeft;
                OnDownLeft.Invoke();
            }
            else{
                axisX.axisState = ETCAxis.AxisState.None;
            }
        }
        else if (axisX.axisState!= ETCAxis.AxisState.None) {
            if (axisX.axisValue*coef>0){
                axisX.axisState = ETCAxis.AxisState.PressRight;
                OnPressRight.Invoke();
            }
            else if (axisX.axisValue*coef<0){
                axisX.axisState = ETCAxis.AxisState.PressLeft;
                OnPressLeft.Invoke();
            }
            else{
                axisX.axisState = ETCAxis.AxisState.None;
            }
        }

        coef =1;
        if (axisY.invertedAxis) coef = -1;
        if (Mathf.Abs(OldTmpAxis.y)< axisY.axisThreshold && Mathf.Abs(axisY.axisValue)>=axisY.axisThreshold  ){
            
            if (axisY.axisValue*coef>0){
                axisY.axisState = ETCAxis.AxisState.DownUp;
                OnDownUp.Invoke();
            }
            else if (axisY.axisValue*coef<0){
                axisY.axisState = ETCAxis.AxisState.DownDown;
                OnDownDown.Invoke();
            }
            else{
                axisY.axisState = ETCAxis.AxisState.None;
            }
        }
        else if (axisY.axisState!= ETCAxis.AxisState.None) {
            if (axisY.axisValue*coef>0){
                axisY.axisState = ETCAxis.AxisState.PressUp;
                OnPressUp.Invoke();
            }
            else if (axisY.axisValue*coef<0){
                axisY.axisState = ETCAxis.AxisState.PressDown;
                OnPressDown.Invoke();
            }
            else{
                axisY.axisState = ETCAxis.AxisState.None;
            }
        }
        #endregion

    }        
    #endregion

    #region Touch manager
    private bool isTouchOverJoystickArea(ref Vector2 localPosition, ref Vector2 screenPosition){
        
        bool touchOverArea = false;
        bool doTest = false;
        screenPosition = Vector2.zero;
        
        int count = GetTouchCount();
        int i=0;
        while (i<count && !touchOverArea){
            #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WINRT || UNITY_BLACKBERRY) && !UNITY_EDITOR) 
            if (Input.GetTouch(i).phase == TouchPhase.Began){
                screenPosition = Input.GetTouch(i).position;
                doTest = true;
            }
            #else
            if (Input.GetMouseButtonDown(0)){
                screenPosition = Input.mousePosition;
                doTest = true;

            }
            #endif
            
            if (doTest && isScreenPointOverArea(screenPosition, ref localPosition) ){
                touchOverArea = true;
            }
            
            i++;
        }
        
        return touchOverArea;
    }
    
    private bool isScreenPointOverArea(Vector2 screenPosition, ref Vector2 localPosition){
        
        bool returnValue = false;
        
        if (joystickArea != JoystickArea.UserDefined){
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle( cachedRootCanvas.rectTransform(),screenPosition,null,out localPosition)){
                
                switch (joystickArea){
                case JoystickArea.Left:
                    if (localPosition.x<0){
                        returnValue=true;
                    }
                    break;
                    
                case JoystickArea.Right:
                    if (localPosition.x>0){
                        returnValue = true;
                    }
                    break;
                    
                case JoystickArea.FullScreen:
                    returnValue = true;
                    break;
                    
                case JoystickArea.TopLeft:
                    if (localPosition.y>0 && localPosition.x<0){
                        returnValue = true;
                    }
                    break;
                case JoystickArea.Top:
                    if (localPosition.y>0){
                        returnValue = true;
                    }
                    break;
                    
                case JoystickArea.TopRight:
                    if (localPosition.y>0 && localPosition.x>0){
                        returnValue=true;
                    }
                    break;
                    
                case JoystickArea.BottomLeft:
                    if (localPosition.y<0 && localPosition.x<0){
                        returnValue = true;
                    }
                    break;
                    
                case JoystickArea.Bottom:
                    if (localPosition.y<0){
                        returnValue = true;
                    }
                    break;
                    
                case JoystickArea.BottomRight:
                    if (localPosition.y<0 && localPosition.x>0){
                        returnValue = true;
                    }
                    break;
                }
            }
        }
        else{
            if (RectTransformUtility.RectangleContainsScreenPoint( userArea, screenPosition, cachedRootCanvas.worldCamera )){
                RectTransformUtility.ScreenPointToLocalPointInRectangle( cachedRootCanvas.rectTransform(),screenPosition,cachedRootCanvas.worldCamera,out localPosition);
                returnValue = true;
            }
        }
        
        return returnValue;
        
    }
    
    private int GetTouchCount(){
        #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WINRT || UNITY_BLACKBERRY) && !UNITY_EDITOR) 
        return Input.touchCount;
        #else
        if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)){
            return 1;
        }
        else{
            return 0;
        }
        #endif
    }
    #endregion

    #region Other private method
    public float GetRadius(){

        float radius =0;
        
        switch (radiusBase){
        case RadiusBase.Width:
            radius = cachedRectTransform.sizeDelta.x * 0.5f;
            break;
        case RadiusBase.Height:
            radius = cachedRectTransform.sizeDelta.y * 0.5f;
            break;
        case RadiusBase.UserDefined:
            radius = radiusBaseValue;
            break;
        }
        
        return radius;
    }

    protected override void SetActivated (){
    
        GetComponent<CanvasGroup>().blocksRaycasts = _activated;

        if (!_activated){
            OnUp(false);
        }
    }

    protected override void SetVisible (bool visible=true){

        bool localVisible = _visible;
        if (!visible){
            localVisible = visible;
        }
        GetComponent<Image>().enabled = localVisible;
        thumb.GetComponent<Image>().enabled = localVisible;
        GetComponent<CanvasGroup>().blocksRaycasts = _activated;


    }
    #endregion


    private void DoTurnAndMove(){

        float angle =Mathf.Atan2( axisX.axisValue,axisY.axisValue ) * Mathf.Rad2Deg;
        float speed = tmMoveCurve.Evaluate( new Vector2(axisX.axisValue,axisY.axisValue).magnitude) * tmSpeed;

        if (axisX.directTransform != null){

            axisX.directTransform.rotation = Quaternion.Euler(new Vector3(0,  angle + tmAdditionnalRotation,0));

            if (axisX.directCharacterController != null){
                if (axisX.directCharacterController.isGrounded || !tmLockInJump){
                    Vector3 move = axisX.directCharacterController.transform.TransformDirection(Vector3.forward) *  speed;
                    axisX.directCharacterController.Move(move* Time.deltaTime);
                    tmLastMove = move;
                }
                else{
                    axisX.directCharacterController.Move(tmLastMove* Time.deltaTime);
                }
            }
            else{
                axisX.directTransform.Translate(Vector3.forward *  speed * Time.deltaTime,Space.Self);
            }
        }

    }

    public void InitCurve(){
        axisX.InitDeadCurve();
        axisY.InitDeadCurve();
        InitTurnMoveCurve();
    }

    public void InitTurnMoveCurve(){
        tmMoveCurve = AnimationCurve.EaseInOut(0,0,1,1);
        tmMoveCurve.postWrapMode = WrapMode.PingPong;
        tmMoveCurve.preWrapMode = WrapMode.PingPong;
    }
}

 

标签:axisValue,Vector2,axisX,axisY,摇杆,unity,&&,public
From: https://www.cnblogs.com/zqiang0803/p/18452166

相关文章

  • Unity 从零开始的框架搭建1-1 unity中对象调用的三种方式的优缺点分析【干货】
    该文章专栏是向QFrameWork作者凉鞋老师学习总结得来,吃水不忘打井人,不胜感激Unity框架搭建学习笔记1-1,前一个1代表凉鞋的第一季教程,后一个1代表该季第一篇我的文章 unity中对象调用的三种方式方法调用,例如:A调用B的SayHello方法委托或者回调,例如:界面监听子按钮的点击......
  • Unity移动旋转缩放
    //【移动】//一、修改Position//Translate//每帧移动一定距离,乘以deltaTime后变成每秒移动一定距离,默认是局部坐标会受自身旋转影响//transform.Translate(Vector3.up*Time.deltaTime);//transform.Translate(Vector3......
  • 【Unity】绘制表格:排行榜
    绘制一款表格,表格特征为头行颜色不同、间隔行不同背景色、滚动视图;表格内容是分数排行榜,前三名有奖牌图标前缀。成果展示Scene部分标题和背景滚动视图:表头、表格行模板效果图脚本部分脚本绑定在滚动组件的Viewport物体上。publicclassHighscoreTable:MonoBehaviour......
  • Unity 二维数组序列化
    unity中,二维以上的数量是不支持序列化的,如:usingSystem.Collections.Generic;usingUnityEngine;publicclassTestArray:MonoBehaviour{//不支持序列化(在Inspector面板无法显示)publicRect[][]rect2Ds;//不支持序列化(在Inspector面板无法显示)//pub......
  • 【春秋招必看】Unity相关笔试面试题(内有完整答案)第12期
    欢迎来到光光的奇妙冒险,我是你们的煎饼光子老师。今天是我们的第12期试题总结。C#部分:(前四题为字节跳动面试问题)1、List是链表还是数组答案:数组2、上一题追问:当list满了的时候,我再加入一个元素它的效率会比较低啊,它内部大概是一个什么样的执行过程呢?答案:数组搬家,会......
  • unity性能优化(有关图集)
    1.什么是图集?首先,你必须把你的美术资源TextureType改为Sprite(精灵类型),因为SpriteAltas只支持Sprite这种TextureType格式。官方:2D项目使用精灵和其他图形来创建其场景的视觉效果。这意味着单个项目可能包含许多纹理文件。Unity通常会为场景中的每个纹理发出一个绘制调用;但是,......
  • 【花雕学编程】Arduino动手做(232)---ESP32-S3 CAM使用joystick双轴摇杆模块控制LED灯
    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来——小小的......
  • Unity实战案例全解析:RTS游戏的框选和阵型功能(5)阵型功能 优化一
    前篇:Unity实战案例全解析:RTS游戏的框选和阵型功能(4)阵型功能-CSDN博客本案例来源于unity唐老狮,有兴趣的小伙伴可以去泰克在线观看该课程我只是对重要功能进行分析和做出笔记分享,并未无师自通,吃水不忘打井人本案例的实现流程图 兵种排序  首先在一个阵型中,我们希望远......
  • unity编辑器绘制扇形
    使用UnityEditor.Handles.DrawSolidArc usingUnityEngine;usingUnityEditor;publicclassDrawSectorHandles:MonoBehaviour{publicfloatradius=5f;//扇形半径publicfloatangle=90f;//扇形角度publicColorarcColor=Color.red;/......
  • Unity编辑器扩展-基于UniTask的Http请求和下载
    publicclassHttpHelper{publicstaticasyncUniTask<(bool,string)>RequestGetUrlAsync(stringurl,booldebug=false){Debug.Log($"reqgeturl:{url}");UnityWebRequestreq=null;......