1. 程式人生 > >unity ugui 傷害飄血

unity ugui 傷害飄血

    public virtual void OnUpdate()
    {
        if (_thissprite.goRoot.hudTrm == null)
            return;
        this._startWorldPos = _thissprite.goRoot.hudTrm.position + offsetVec;
        var screenPos = this.getScreenPos(this._mainCamera, this._startWorldPos);
        this.transform.localPosition = new Vector3(screenPos.x + this._xOffset, screenPos.y, 0);
    }
   // 將精靈的世界座標轉換成螢幕座標
    public Vector3 getScreenPos(Camera cam, Vector3 worldPos)
    {
        var resolutionX = this._canvasScaler.referenceResolution.x;
        var resolutionY = this._canvasScaler.referenceResolution.y;
        var offset = (Screen.width / this._canvasScaler.referenceResolution.x) * (1 - this._canvasScaler.matchWidthOrHeight) + (Screen.height / this._canvasScaler.referenceResolution.y) * this._canvasScaler.matchWidthOrHeight;
        var screenPos = RectTransformUtility.WorldToScreenPoint(cam, worldPos);
        return new Vector3(screenPos.x / offset, screenPos.y / offset, 0);
    }