82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import { _decorator, Color, Component, Node, Size, Sprite, UITransform } from 'cc';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('BasicGeometry')
|
||
export class BasicGeometry extends Component {
|
||
private spriteNode: Node = null;
|
||
private spriteComponent: Sprite = null;
|
||
private uiTransform: UITransform = null;
|
||
|
||
start() {
|
||
// 获取名为"Sprite"的子节点
|
||
this.spriteNode = this.node.getChildByName("Sprite");
|
||
if (this.spriteNode) {
|
||
this.spriteComponent = this.spriteNode.getComponent(Sprite);
|
||
this.uiTransform = this.spriteNode.getComponent(UITransform);
|
||
}
|
||
|
||
if (!this.spriteNode || !this.spriteComponent) {
|
||
console.warn("BasicGeometry: 未找到名为'Sprite'的子节点或Sprite组件");
|
||
}
|
||
|
||
if (!this.uiTransform) {
|
||
console.warn("BasicGeometry: 未找到UITransform组件");
|
||
}
|
||
}
|
||
|
||
update(deltaTime: number) {
|
||
|
||
}
|
||
|
||
/**
|
||
* 设置精灵的大小
|
||
* @param width 宽度
|
||
* @param height 高度
|
||
*/
|
||
public SetSize(width: number, height: number): void {
|
||
if (!this.uiTransform) {
|
||
console.warn("BasicGeometry: UITransform组件不存在,无法设置大小");
|
||
return;
|
||
}
|
||
|
||
this.uiTransform.setContentSize(new Size(width, height));
|
||
}
|
||
|
||
/**
|
||
* 设置精灵的颜色
|
||
* @param color 颜色值
|
||
*/
|
||
public SetColor(color: Color): void {
|
||
if (!this.spriteComponent) {
|
||
console.warn("BasicGeometry: Sprite组件不存在,无法设置颜色");
|
||
return;
|
||
}
|
||
|
||
this.spriteComponent.color = color;
|
||
}
|
||
|
||
/**
|
||
* 获取当前精灵的大小
|
||
* @returns 返回当前大小,如果组件不存在则返回null
|
||
*/
|
||
public GetSize(): Size | null {
|
||
if (!this.uiTransform) {
|
||
return null;
|
||
}
|
||
return this.uiTransform.contentSize;
|
||
}
|
||
|
||
/**
|
||
* 获取当前精灵的颜色
|
||
* @returns 返回当前颜色,如果组件不存在则返回null
|
||
*/
|
||
public GetColor(): Color | null {
|
||
if (!this.spriteComponent) {
|
||
return null;
|
||
}
|
||
return this.spriteComponent.color;
|
||
}
|
||
}
|
||
|
||
|