Sunday, June 29, 2025
spot_img

Generally the hyperlink to a element breaks. Is it the element? – Cocos Creator


Hello all!

I take advantage of Cocos Creator model 3.8.6.

I’ve my very own IconView element in my undertaking, it’s situated within the following path:

property/scripts/coreView/IconView.ts

I don’t know precisely underneath what circumstances, however typically the hyperlink to this element turns into invalid. I simply begin the sport and get an error that I’m making an attempt to name a operate on an object that’s not assigned.

This doesn’t occur usually, but it surely’s nonetheless disagreeable. For instance, I’m working with some a part of the UI, inserting fully various things, assigning hyperlinks, saving the prefab (often I then shut the prefab), launching the scene and getting an error (though I didn’t contact the IconView hyperlinks manually). So far as I perceive, this solely occurs if I open the prefab. I don’t suppose I’ve ever had hyperlinks get tousled like that in prefabs that I haven’t opened not too long ago. Afterwards, I’ve to open the prefab and assign the hyperlinks once more (within the inspector it reveals as if I didn’t set something, and never a lacking object).

To be sincere, I’m already uninterested in this. I observed this since model 3.8.3.

I’ve many elements in my undertaking, however I solely bought this error with three of my elements. Two of them are gone – solely IconView stays.

What’s unsuitable with him?

import { Shade, Element, Enum, Dimension, Sprite, SpriteFrame, UITransform, _decorator } from "cc";
const { ccclass, property } = _decorator;

export enum IconFillingMethod {
	filling,																	// Could exit of space
	becoming																		// Will likely be included within the space
}
Enum(IconFillingMethod);

@ccclass('IconView')
export class IconView extends Element {
	@property({ kind: UITransform })
    personal areaUITransform: UITransform;
	@property({ kind: UITransform })
    personal iconUITransform: UITransform;
	@property({ kind: Sprite })
    personal icon: Sprite;
	@property({ kind: IconFillingMethod })
    personal fillingMethod: IconFillingMethod = IconFillingMethod.becoming;

	public SetIcon(sprite: SpriteFrame): void {
		this.icon.spriteFrame = sprite;
		this.UpdateSize();
	}

	public SetIconWithOriginalSize(sprite: SpriteFrame, scale: quantity = 1): void {
		this.areaUITransform.setContentSize(sprite.rect.width * scale, sprite.rect.peak * scale);
		this.icon.spriteFrame = sprite;
		this.UpdateSize();
	}

	public GetIcon(): SpriteFrame {
		return this.icon.spriteFrame;
	}

	public GetIconSize(): Readonly {
		return this.iconUITransform.contentSize;
	}

	public SetIconColor(colour: Readonly): void {
		this.icon.colour = colour;
	}

	public GetIconColor(): Readonly {
		return this.icon.colour;
	}

	public SetGrayscale(isGrayscale: boolean): void {
		this.icon.grayscale = isGrayscale;
	}

	personal UpdateSize(): void {
		let areaWidth = this.areaUITransform.contentSize.width;
		let areaHeight = this.areaUITransform.contentSize.peak;
		let originalSpriteWidth = this.icon.spriteFrame.rect.width;
		let originalSpriteHeight = this.icon.spriteFrame.rect.peak;

		swap(this.fillingMethod) {
			case IconFillingMethod.filling:
				this.CalcProportionalRectFillingSize(areaWidth, areaHeight, originalSpriteWidth, originalSpriteHeight);
				break;
			case IconFillingMethod.becoming:
				this.CalcProportionalRectFittingSize(areaWidth, areaHeight, originalSpriteWidth, originalSpriteHeight);
				break;
		}
	}

	personal CalcProportionalRectFillingSize(fillableWidth: quantity, fillableHeight: quantity, spriteWidth: quantity, spriteHeight: quantity): void {
		let width = 0;
		let peak = 0;
		
		let aspectRatio = spriteWidth / spriteHeight;
		
		width = fillableWidth;
		peak = width / aspectRatio;
		
		if (peak  fillableHeight) {
			peak = fillableHeight;
			width = peak * aspectRatio;
		}
		
		this.iconUITransform.setContentSize(width, peak);
	}
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles